Hello Everyone!!
Today’s session was really interesting because we learned about one of the most important concepts in React – Hooks. Let’s understand each topic step by step in a simple and clear way.
1.Introduction to Hooks:
Hooks are special functions in React. They let us "hook into" React features like state and lifecycle methods in functional components.
Before Hooks, only class components could use state, but now functional components can do everything with the help of Hooks!
2.What is a Hook?
A Hook is a JavaScript function that allows you to use React features (like state, context, refs) inside a functional component.
Note: Hooks only work in functional components, not in regular JavaScript functions or class components.
3.Why use Hooks? (Benefits of Hooks)
- Easy to use state and lifecycle methods in functional components.
- Avoid writing class components.
- Code becomes cleaner, shorter, and more readable.
- You can reuse stateful logic using custom hooks.
4.Types of Hooks
React provides some built-in hooks like:
useState()
useEffect()
-
useContext()
, etc.
In today's class, we focused on useState()
5.What is useState()?
useState()
is the most used React Hook.
It lets you add and manage state in a functional component.
Syntax
:
const [stateVariable, setStateFunction] = useState(initialValue);
-
stateVariable
: To hold the data. -
setStateFunction
: To update the data. -
initialValue
: The starting value of the state.
6.Example of useState():
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0); // Initial value is 0
return (
<div>
<h2>Count: {count}</h2>
<button onClick={() => setCount(count + 1)}>Increase</button>
</div>
);
}
export default Counter;
Explanation:
-
count
is the state variable. -
setCount
is the function to update thecount
. - Initially, count = 0.
- On button click, count increases by 1.
7.How to count value in JSX?
You can directly use state variables in JSX using {}
brackets.
Example
:
<h2>Count: {count}</h2>
Whenever the count
state updates, this value will automatically re-render in JSX.
8.Mini Project – Understanding Hooks
Today, we started a mini project (Counter App) using the useState()
hook.
How to Understand?
- Break the problem into small parts.
- Think about what data (state) you need.
- Use
useState()
to store that data. - Handle user actions (like button clicks) with functions.
- Render the state in JSX to see the result.
Recap Day 3:
- Introduction to Hooks.
- Why Hooks are important?
- useState Hook explained with example.
- How to count value in JSX.
- Started mini project – Counter App.
References:
[(https://legacy.reactjs.org/docs/hooks-intro.html)]
Learn Daily!! Build Better!! Stay Curious!!
Top comments (4)
Been cool seeing steady progress - it adds up. what do you think actually keeps things growing over time? habits? luck? just showing up?
Thank you! I believe it's all about showing up consistently and building good habits. Small steps make a big difference over time!
Nice breakdown, especially on making useState feel simple! Did you try combining useState with useEffect yet in your mini project?
Thanks! Yes, I’ve started using useEffect with useState — super useful for handling side effects!
Some comments may only be visible to logged-in visitors. Sign in to view all comments.