Hooks Introduction
Learn what React Hooks are, why they are used, and how they help functional components manage state and other React features.
1. What Are React Hooks?
React Hooks are special functions that allow functional components to use React features such as state, side effects, references, and context.
Hooks were introduced in React 16.8. Before Hooks, state management and lifecycle features were commonly handled using class components. Hooks allow developers to use many of these features directly inside functional components.
use, such as useState,
useEffect, and useRef.
2. Why Do We Use React Hooks?
React Hooks make it easier to manage component state, handle side effects, reuse stateful logic, and work with React features inside functional components.
- Manage and update component state.
- Perform side effects such as fetching data.
- Access and share values through React Context.
- Store references to values or DOM elements.
- Reuse logic through custom Hooks.
3. How React Hooks Work
A functional component calls a Hook to access a particular React feature. React manages that feature and updates the component when necessary.
useState,
React schedules a re-render so the UI can display the updated
value.
4. Commonly Used React Hooks
useState
Used to add and update state in a functional component.
useEffect
Used to synchronize a component with external systems, such as browser APIs, subscriptions, or network requests.
useRef
Used to keep a mutable value between renders or reference a DOM element without causing a re-render when the reference changes.
useContext
Used to read values provided by a React Context.
useReducer
Used to manage state through a reducer function, especially when state transitions involve more complex logic.
useMemo and useCallback
Used to cache calculated values and function definitions when memoization is useful for performance.
Custom Hooks
Custom Hooks allow developers to reuse stateful logic across multiple functional components.
5. Simple Example of a React Hook
The following example uses useState to store
and update a counter value.
Example Code
import { useState } from "react";
function App() {
const [count, setCount] = useState(0);
return (
<div>
<h2>React Hooks Example</h2>
<p>Count: {count}</p>
<button onClick={() =>
setCount(count + 1)
}>
Increase Count
</button>
</div>
);
}
export default App;
Expected Output
React Hooks Example
Count: 0
After clicking Increase Count once, the count becomes 1.
useState(0) Hook initializes the counter
with the value 0. The
setCount() function updates the state, and React
re-renders the component to display the new count.
6. Rules of React Hooks
- Call Hooks at the top level of a functional component or another custom Hook.
- Do not call Hooks inside loops, conditions, or nested functions.
- Call Hooks only from React function components or custom Hooks.
- Do not call Hooks from ordinary JavaScript functions that are not custom Hooks or React components.
7. Practice Tasks
Complete the following practice tasks:
- Explain React Hooks in your own words.
- List five commonly used React Hooks.
- Create a functional component that uses useState.
- Create a counter with an Increase button.
- Explain why Hooks should not be called inside conditions.