React Hooks are special functions that let us use React features in functional components.
For Example:
Store and update data.
Handle side effects.
Access other React features. Common React Hooks are useState, useEffect, useContext, and useRef.
useState Hook:
useState is used to store and update a value.
Example:
Counter app.
import { useState } from "react";
function App() {
const [count, setCount] = useState(0);
return (
Count: {count}
setCount(count + 1)}>
Click Me
);
}
export default App;
Try the counter
0
β Decrease+ IncreaseReset
Explanation:
useState(0) β initializes the count to 0.
count β stores the current value.
setCount β updates the value.
onClick β runs when the button is clicked.
2.Common React Hooks:
Hook Purpose
useState Store and update data
useEffect Perform side effects, such as fetching data
useContext Access shared data
useRef Keep a reference to a value or DOM element
useMemo Cache a calculated result
useCallback Cache a function definition
useEffectβ¦