React Hooks are functions that let you use state and other React features in functional components. Before hooks were introduced in React 16.8, state and lifecycle methods were only available in class components. Hooks allow you to manage state, perform side effects, and access other React features without needing to write class components, making it easier to write and maintain functional components
Let's use an analogy to better understand the role of Hooks in React. Imagine you are running a restaurant, functional components are the chefs in your kitchen responsible for preparing different dishes (UI parts). They are good at cooking (rendering) but initially, they needed help remembering previous orders(no state) or doing anything beyond cooking (no side effects). but then you introduce special tools and techniques that make your chefs more versatile and efficient.
Key Hooks and analogies to explain them better
- ‘useState’(memory Notebook)
Manages state in a functional component.
Returns an array with the current state and function to update it.
Imagine giving each chef a memory notebook(‘useState’). Now they can write down important information like several dishes they have cooked so far. When a new order comes in they can check their notebook and update it accordingly.
- ‘useEffect’ (Daily Checklist and Timers)
Performs side effects in functional components(eg., data fetching, subscription, manually changing the DOM)
Can mimic lifecycle methods like ‘componentDidMount’,’componentDidUpdate’, and ‘componentWillUnmount’.
You provide your chef with a daily checklist (‘useEffect’). This checklist includes tasks like fetching ingredients every morning or cleaning the kitchen at the end of the day. They can also set timers to remind them to check the oven or call a supplier.
- ‘useContext’ (walkie-talkies)
Accesses the value of React context.
You give chefs walkie-talkies (‘useContext’) so that they can communicate with each other and share important information like today's special dish without yelling across the kitchen
- ‘useReducer’(Recipe Book)
Manages more complex state logic than ‘useState’ similar to Redux but local component state
For more complex dishes you give your chefs a detailed recipe book (‘useReducer’). This book includes step-by-step instructions and variations of the recipes based on different customer requests.
- ‘useMemo’ (pre-prepared ingredients)
Memorize a value to avoid recomputing it on every render.
To save time you can allow chefs to prepare some ingredients in advance and store them in the fridge (‘useMemo’). They can use these pre-pared ingredients whenever needed saving time during rush hours.
- ‘useCallback’ (pre-set cooking techniques)
Memorizes a callback function to prevent unnecessary re-renders.
You teach chefs specific cooking techniques and let them store these techniques (‘useCallback’) so they don't have to figure out the steps every time they cook the same dish.
- ‘useRef’ (kitchen tools)
They are used to access the DOM elements directly or persist a mutable value across renders.
You give chefs special kitchen tools (‘useRef’)like a timer so that they can refer to the time. These tools help them keep track of things that don't need to be changed often like monitoring the temperature of the oven.