React Hooks

React Hooks have revolutionized the way we build React applications, offering a more powerful and flexible way to manage state and side effects in functional components. This guide will help you master the art and science of React Hooks, equipping you with the knowledge to create more maintainable and concise React apps.

1. What is a Hook?

A Hook is a special function that lets you “hook into” React features. Essentially, it allows you to use state and other React features without writing a class. This is revolutionary because it simplifies component logic and improves code reusability.

2. Hook Rules

Before diving into specific Hooks, it’s important to understand two key rules:

  • Only Call Hooks at the Top Level: Don’t call Hooks inside loops, conditions, or nested functions.
  • Only Call Hooks from React Functions: Call them from within React functional components, not regular JavaScript functions.

3. React useState Hook

The useState Hook lets you add state to functional components. It returns a stateful value and a function to update it.

import React, { useState } from 'react';

function Example() {
  const [count, setCount] = useState(0);

  return (
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me

4. React useEffect Hook

useEffect lets you perform side effects in functional components. It’s a powerful Hook for data fetching, subscriptions, or manually changing the DOM.

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

function Example() {
  const [data, setData] = useState([]);

  useEffect(() => {
    fetchData().then(data => setData(data));
  }, []); // The empty array means this effect runs once on mount

  // Fetching data function here

5. React useContext Hook

useContext lets you access the value of a React context object. It’s useful for passing data deeply throughout the component tree without explicit props.

import React, { useContext } from 'react';
import { ThemeContext } from './theme-context';

function ThemedButton() {
  const theme = useContext(ThemeContext);
  return <button style={{ background: theme.background, color: theme.foreground }}>I am styled by theme context!</button>;

6. React useRef Hook

useRef returns a mutable ref object whose .current property is initialized to the passed argument. It’s handy for keeping a mutable value that does not cause re-render when changed.

import React, { useRef } from 'react';

function TextInputWithFocusButton() {
  const inputEl = useRef(null);
  const onButtonClick = () => {
  return (
      <input ref={inputEl} type="text" />
      <button onClick={onButtonClick}>Focus the input</button>

7. React useReducer Hook

useReducer is an alternative to useState. It’s preferable when you have complex state logic involving multiple sub-values or when the next state depends on the previous one.

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 };
      throw new Error();

function Counter() {
  const [state, dispatch] = useReducer(reducer, { count: 0 });

  return (
      Count: {state.count}
      <button onClick={() => dispatch({ type: 'increment' })}>+</button>
      <button onClick={() => dispatch({ type: 'decrement' })}>-</button>

8. React useCallback Hook

useCallback returns a memoized callback function. It’s useful to prevent unnecessary re-renders and for optimizing child component rendering performance.

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

function Example() {
  const [count, setCount] = useState(0);
  const memoizedCallback = useCallback(() => {
  }, [count]);

  // Function to do something here

9. React useMemo Hook

useMemo returns a memoized value. It’s used to optimize performance by memorizing computationally expensive calculations.

import React, { useMemo } from 'react';

function Example({ number }) {
  const memoizedValue = useMemo(() => computeExpensiveValue(number), [number]);

  // Function to compute expensive value here

10. React Custom Hooks

Custom Hooks are JavaScript functions that use Hooks inside them and enable you to create your reusable stateful logic.

import { useState, useEffect } from 'react';

function useCustomHook() {
  const [data, setData] = useState(null);

  useEffect(() => {
  }, []);

  return data;

In conclusion, React Hooks offer a powerful and elegant way to handle state and side effects in your React components. By understanding and properly utilizing these hooks, you can write cleaner, more maintainable, and more efficient React code. Keep exploring and experimenting with these Hooks to fully leverage their potential in your projects.