React Hooks: The Oddball Method to Building Scalable Components
What is a Hook?
Think of a hook as a toolbox. When you need to fix something, you open the toolbox and pick out the tools you need. Similarly, when you need to manage state or perform a side effect, you use a hook. Therefore :
A hook is a function that lets you use state and other React features without writing a class. Hooks are used in functional components and also make it easier to reuse stateful logic across components.
Rules of Working with Hooks
Hooks must be used at the top level of a functional component or custom hook.
Hooks must not be called conditionally.
Hooks must not be called in loops.
Hooks must not be called from regular JavaScript functions
Advantages Of Working With Hooks
Simpler code: Hooks make it easier to write and understand functional components, which are simpler than class components.
Reusable logic: Hooks make it easy to reuse stateful logic across components.
Better performance: Hooks provide a more optimized way of managing state and effects compared to class components, leading to better performance.
Basic Hooks
Usestate()
The useState() hook is used to manage state in a functional component. It takes an initial state as an argument and returns an array with the current state and a function to update the state.
Use Case: This hook is useful for managing simple state in a component, such as a form input value.
import React, { useState } from 'react'; function Example() { const [count, setCount] = useState(0); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}> Click me </button> </div> ); }
UseEffect()
The useEffect() hook is used to perform side effects in a functional component, such as fetching data or updating the DOM. It takes a function as an argument and runs it after every render.
Use Case: This hook is useful for fetching data from an API and updating the component when the data changes.
import React, { useState, useEffect } from 'react'; function Example() { const [data, setData] = useState([]); useEffect(() => { fetch('https://api.example.com/data') .then(response => response.json()) .then(data => setData(data)); }, []); return ( <ul> {data.map(item => ( <li key={item.id}>{item.name}</li> ))} </ul> ); }
UseContext()
The useContext() hook is used to access data from a parent component without passing it through props. It takes a context object as an argument and returns the current value of the context.
Use Case: This hook is useful for accessing global data, such as a user's authentication status or theme preferences.
import React, { useContext } from 'react'; import { UserContext } from './UserContext'; function Example() { const { user } = useContext(UserContext); return ( <div> <p>Hello, {user.name}!</p> </div> ); }
Advanced Hooks
useCallback()
The useCallback() hook is used to memoize a function and prevent unnecessary re-renders in a component. It takes a function and a list of dependencies as arguments and returns a memoized function.
Use Case: This hook is useful for optimizing performance in components that render frequently, such as a list of items.
import React, { useState, useCallback } from 'react'; function Example() { const [count, setCount] = useState(0); const increment = useCallback(() => { setCount(count + 1); }, [count]); return ( <div> <p>You clicked {count} times</p> <button onClick={increment}> Click me </button> </div> ); }
useRef()
The useRef() hook is used to create a mutable reference that persists across renders. It takes an initial value and returns a ref object with a current property that can be modified.
Use Case: This hook is useful for accessing and modifying DOM elements or values that persist across renders, such as a timer ID.
import React, { useRef } from 'react'; function Example() { const intervalRef = useRef(null); function startTimer() { intervalRef.current = setInterval(() => console.log('Tick'), 1000); } function stopTimer() { clearInterval(intervalRef.current); } return ( <div> <button onClick={startTimer}>Start</button> <button onClick={stopTimer}>Stop</button> </div> ); }
useReducer()
The useReducer() hook is an alternative to useState() that provides a more powerful way of managing complex state in a component. It takes a reducer function and an initial state as arguments and returns the current state and a dispatch function to update the state.
Use Case: This hook is useful for managing complex state, such as a list of items with different properties, that requires more than just simple updates like adding or removing items to and from the cart.
import React, { useReducer } from 'react';
const cartReducer = (state, action) => {
switch (action.type) {
case 'ADD_ITEM':
return { ...state, items: [...state.items, action.payload] };
case 'REMOVE_ITEM':
return { ...state, items: state.items.filter(item => item.id !== action.payload) };
default:
throw new Error();
}
};
const Shoppingcart = () => {
const [cart, dispatch] = useReducer(cartReducer, { items: [] });
const handleAddToCart = (item) => {
dispatch({ type: 'ADD_ITEM', payload: item });
};
const handleRemoveFromCart = (itemId) => {
dispatch({ type: 'REMOVE_ITEM', payload: itemId });
};
return (
<div>
<ul>
{cart.items.map(item => (
<li key={item.id}>
{item.name} - ${item.price}
<button onClick={() => handleRemoveFromCart(item.id)}>Remove</button>
</li>
))}
</ul>
<button onClick={() => handleAddToCart({ id: 1, name: 'Item 1', price: 10 })}>Add Item
</button>
</div>
);
};
export default Shoppingcart;
Conclusion
React Hooks provide an easy and efficient way of managing state and side effects in functional components. With basic hooks like useState(), useEffect(), and useContext(), developers can easily manage simple state and perform side effects.
Advanced hooks like useReducer(), useRef(), and useCallback() provide more powerful ways of managing complex state and optimizing performance. By using React Hooks, developers can write simpler, reusable, and performant code.
Reference Resources
Official React Hooks Documentation - https://reactjs.org/docs/hooks-intro.html
React Hooks Tutorial for Beginners - https://www.smashingmagazine.com/2020/09/react-hooks-api-guide/
React Hooks: What Are They and How Do You Use Them? - https://www.telerik.com/blogs/react-hooks-what-are-they-and-how-do-you-use-them
Subscribe to my newsletter
Read articles from Anita Kahenya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Anita Kahenya
Anita Kahenya
A passionate technophile who also happens to be a technical mentor, and workout enthusiast with a love for all things life from Kenya.