Exploring React Hooks: Simplifying State Management in Function Components

Dev Balaji
5 min readApr 14, 2023

--

React Hooks is a feature introduced in React 16.8 that allows developers to use state and other React features without writing a class component. Hooks are functions that let developers use React features in function components.

There are several built-in hooks in React, each designed for a specific purpose. Here are some of the most commonly used React Hooks:

  1. useState Hook: This hook allows developers to add state to their function components. It returns an array that contains the current state value and a function to update that state.

Example:

import React, { useState } from 'react';

function Counter() {
const [count, setCount] = useState(0);
function increment() {
setCount(count + 1);
}
return (
<div>
<p>You clicked {count} times</p>
<button onClick={increment}>Click me</button>
</div>
);
}

2. useEffect Hook: This hook allows developers to add side effects to their function components. It is used to perform actions after the component has been rendered.

Example:

import React, { useState, useEffect } from 'react';

function MyComponent() {
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>
);
}

3. useContext Hook: This hook allows developers to use context in their function components. Context is used to pass data down the component tree without having to pass props manually at every level.

Example:

import React, { useContext } from 'react';

const MyContext = React.createContext();
function MyComponent() {
const data = useContext(MyContext);
return <div>{data}</div>;
}
function App() {
return (
<MyContext.Provider value="Hello, World!">
<MyComponent />
</MyContext.Provider>
);
}

4. useReducer Hook: This hook allows developers to manage state using a reducer function. It is similar to the useState hook, but it provides a more powerful way to manage complex state.

Example:

import React, { useReducer } from 'react';

function reducer(state, action) {
switch (action.type) {
case 'increment':
return { count: state.count + 1 };
case 'decrement':
return { count: state.count - 1 };
default:
throw new Error();
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, { count: 0 });
return (
<div>
Count: {state.count}
<button onClick={() => dispatch({ type: 'increment' })}>+</button>
<button onClick={() => dispatch({ type: 'decrement' })}>-</button>
</div>
);
}

5. useMemo Hook: The useMemo Hook is used to memoize expensive computations in a functional component. It caches the result of a function until its dependencies change.

Example:

import React, { useMemo, useState } from 'react';

function MyComponent() {
const [value, setValue] = useState('');

function expensiveFunction() {
// some expensive computation here
return result;
}
const memoizedValue = useMemo(() => expensiveFunction(), [value]);
return <div>{memoizedValue}</div>;
}

6. useRef Hook: The useRef Hook is used to create a reference to a DOM element or a value that persists across re-renders. It is similar to using a class instance variable in a class component.

Example:

import React, { useRef } from 'react';

function MyComponent() {
const inputRef = useRef(null);
function handleClick() {
inputRef.current.focus();
}
return (
<div>
<input type="text" ref={inputRef} />
<button onClick={handleClick}>Focus input</button>
</div>
);
}

7. useLayoutEffect Hook: The useLayoutEffect Hook is used to perform side effects immediately after the DOM has been updated. It is similar to useEffect, but it runs synchronously after all DOM mutations.

Example:

import React, { useLayoutEffect, useState } from 'react';

function MyComponent() {
const [width, setWidth] = useState(0);
useLayoutEffect(() => {
const handleResize = () => {
setWidth(window.innerWidth);
};
window.addEventListener('resize', handleResize);
handleResize();
return () => window.removeEventListener('resize', handleResize);
}, []);
return <div>Width: {width}px</div>;
}

8. useImperativeHandle Hook: The useImperativeHandle Hook is used to expose a set of functions to a parent component from a child component that is wrapped in a forwardRef. It is similar to using ref in a class component.

Example:

import React, { forwardRef, useImperativeHandle, useState } from 'react';

const ChildComponent = forwardRef((props, ref) => {
const [value, setValue] = useState('');
useImperativeHandle(ref, () => ({
getValue: () => value,
setValue: (newValue) => setValue(newValue),
}));
return <input type="text" value={value} onChange={event => setValue(event.target.value)} />;
});

function MyComponent() {
const childRef = useRef(null);
function handleClick() {
console.log(childRef.current.getValue());
childRef.current.setValue('');
}
return (
<div>
<ChildComponent ref={childRef} />
<button onClick={handleClick}>Get value and clear input</button>
</div>
);
}

9. useCallback Hook: The useCallback Hook allows you to memoize a function in a functional component. It is similar to using shouldComponentUpdate in a class component.

Example:

import React, { useState, useCallback } from 'react';

function MyComponent() {
const [value, setValue] = useState('');
const handleChange = useCallback(event => {
setValue(event.target.value);
}, []);
return <input type="text" value={value} onChange={handleChange} />;
}

10. useAsync Hook: The useAsync Hook is used to handle asynchronous operations in a more convenient way. It accepts a function that returns a promise and returns an object with properties that indicate the status of the operation.

Example:

import React from 'react';
import useAsync from './useAsync';

function MyComponent() {
const { data, error, isLoading } = useAsync(() =>
fetch('https://api.example.com/data').then(response => response.json())
);
if (isLoading) {
return <div>Loading...</div>;
}
if (error) {
return <div>Error: {error.message}</div>;
}
return <div>{data}</div>;
}

11. useDebounce Hook: The useDebounce Hook is used to debounce a value in a more convenient way. It accepts a value and a delay and returns a debounced value that is updated only after the delay has passed.

Example:

import React, { useState } from 'react';
import useDebounce from './useDebounce';

function MyComponent() {
const [searchTerm, setSearchTerm] = useState('');
const debouncedSearchTerm = useDebounce(searchTerm, 500);
function handleChange(event) {
setSearchTerm(event.target.value);
}
return (
<div>
<input type="text" value={searchTerm} onChange={handleChange} />
<p>Debounced value: {debouncedSearchTerm}</p>
</div>
);
}

12. useIntersectionObserver Hook: The useIntersectionObserver Hook is used to observe changes in the intersection between an element and its parent. It accepts an options object and returns an object with properties that indicate the current intersection status.

Example:

import React, { useRef } from 'react';
import useIntersectionObserver from './useIntersectionObserver';

function MyComponent() {
const ref = useRef(null);
const { isIntersecting } = useIntersectionObserver(ref);
return (
<div ref={ref}>
{isIntersecting ? <p>Visible</p> : <p>Not visible</p>}
</div>
);
}

13. useWindowSize Hook: The useWindowSize Hook is used to get the size of the window in a more convenient way. It returns an object with properties that indicate the current width and height of the window.

Example:

import React from 'react';
import useWindowSize from './useWindowSize';

function MyComponent() {
const { width, height } = useWindowSize();
return (
<div>
<p>Window width: {width}</p>
<p>Window height: {height}</p>
</div>
);
}

Conclusion

React Hooks are a powerful tool for React developers that allow them to use state and other React features in function components. By using Hooks, developers can write cleaner, more concise code and avoid the complexity of class components.

--

--

Dev Balaji
Dev Balaji

Written by Dev Balaji

🚀 Tech Enthusiast | 🌟 Mastering JavaScript & Frameworks | 💡 Sharing Tips & Tricks | 📘 Aspiring Blogger & Architect | 🐍 Python Practitioner

No responses yet