DEV Community

Cover image for React hooks
Rahul Bagal
Rahul Bagal

Posted on • Updated on

React hooks

Welcome to our blog post on using React hooks to build projects! React hooks are a new feature in React that allow developers to use state and other React features in functional components, rather than only in class components. Hooks are a powerful tool that can help make your code more concise and easier to understand. In this post, we will be discussing 4 hooks that can be particularly useful when building projects with React. We will be explaining what each hook is, how to use it, and providing examples of how it can be used in a real-world project. By the end of this post, you should have a solid understanding of how to use these hooks to improve your React development skills. So let's dive in!

1.useState Hook

A. Explanation of what the useState hook is and what it does

The useState hook is a built-in React hook that allows you to manage state within a functional component. Prior to React 16.8, state management was limited to class-based components, but with the introduction of hooks, state can now be managed in functional components as well. The useState hook returns an array with two elements: the current state and a setter function for updating the state.

B. How to use the useState hook in a functional component

To use the useState hook in a functional component, you first need to import it from the 'react' library. You can then call the useState hook and pass in an initial state value. The hook will return an array with two elements: the current state and a setter function for updating the state.

import { useState } from 'react';

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

  return (
    <div>
      <p>The current count is {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

In the above example, we are using the useState hook to manage a piece of state called 'count'. We are initializing the count to be 0 and then using the setCount function to update the count when the button is clicked.

C. Example of how the useState hook can be used to manage state in a real-world project

For example, let's say you're building a to-do app and you want to keep track of the number of items on the user's to-do list. You could use the useState hook to manage the state of the number of items on the list.

import { useState } from 'react';

function TodoApp() {
  const [todoItems, setTodoItems] = useState([]);
  const [numberOfItems, setNumberOfItems] = useState(0);

  const addTodo = (todo) => {
    setTodoItems([...todoItems, todo]);
    setNumberOfItems(numberOfItems + 1);
  }

  const removeTodo = (index) => {
    const newTodoItems = todoItems.filter((_, i) => i !== index);
    setTodoItems(newTodoItems);
    setNumberOfItems(numberOfItems - 1);
  }
  return (
    <div>
      <p>Number of items: {numberOfItems}</p>
      <input type="text" placeholder="Add Todo" onKeyPress={(e) => e.key === 'Enter' ? addTodo(e.target.value) : null}/>
      <ul>
          {todoItems.map((todo, index) => <li key={index} onClick={() => removeTodo(index)}>{todo}</li>)}
      </ul>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

In this example, we're using two different useState hooks to manage different states. One is for the to-do items and the other one is for the number of items on the list. We're also using the addTodo and removeTodo functions to update the
state accordingly when a new to-do item is added or an existing one is removed.

D. Tips and best practices for using the useState hook

  • Keep in mind that the useState hook should only be used for simple state management. For more complex state management, it's recommended to use the useReducer hook or a state management library such as Redux.

  • Always initialize the state with a default value. If you don't provide an initial value, the state will be undefined by default.

  • Be mindful of when you're calling the setter function. If you call it too frequently, it can cause unnecessary re-renders and negatively impact performance.

  • Always use the setter function to update the state, never modify the state directly. This ensures that the component re-renders and the UI is updated correctly.

2.useEffect Hook

A. Explanation of what the useEffect hook is and what it does

  • The useEffect hook allows you to run side effects, such as fetching data or modifying the DOM, in a functional component.

  • It is a way to synchronize a component with an external system, such as a server or the browser's DOM.

  • It allows you to handle lifecycle events similar to componentDidMount, componentDidUpdate, and componentWillUnmount in class components.

B. How to use the useEffect hook in a functional component

  • Import the useEffect hook from the 'react' library

  • Call the useEffect hook inside your functional component and pass it a callback function.

  • The callback function should contain the logic for your side effect.

import { useEffect } from 'react';

function MyComponent() {
  useEffect(() => {
    // side effect logic here
  }, [dependencies]);
  // component code here
}
Enter fullscreen mode Exit fullscreen mode

C. Code example of how the useEffect hook can be used to run side effects, such as fetching data or modifying the DOM, in a real-world project

import { useEffect, useState } from 'react';

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

  useEffect(() => {
    fetch('https://my-api.com/data')
      .then(response => response.json())
      .then(data => setData(data))
      .catch(error => console.log(error));
  }, []);

  return (
    <div>
      {data.map(item => (
        <div key={item.id}>{item.name}</div>
      ))}
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

D. Tips and best practices for using the useEffect hook

  • Be mindful of the dependencies array. You should only include values that the effect function depends on, otherwise the effect will re-run every time the component renders.

  • Use the cleanup function to undo any side effects before the component is unmounted.

  • Try to split complex logic into smaller hooks.

  • Be aware of the order in which effects run. Effects run in the order they are defined, and after the component's render.

  • Try to make your effects pure, so they return the same result if called with the same inputs.

  • Use the useLayoutEffect hook to run layout effects, such as measuring the size of an element.

3.useContext Hook

A. Explanation of what the useContext Hook is and what it does:

  • The useContext hook allows you to access and update context from a functional component.
  • Context is a way to share data between components without having to pass props down through every level of the component tree.
  • The useContext hook makes it easy to access context in a functional component without having to use a class component or the Consumer component.

B. How to use the useContext Hook in a functional component code:

  • First, create a context using the createContext function.
    Then, provide the context using the Provider component.

  • In the functional component where you want to access the context, import the useContext hook and call it with the context as the argument.

  • The hook returns the current context value and re-renders the component when the context value changes.
    Copy code

import { createContext, useContext } from 'react';

const MyContext = createContext();

function ParentComponent() {
    const [value, setValue] = useState("Initial Value");

    return (
        <MyContext.Provider value={value}>
            <ChildComponent />
        </MyContext.Provider>
    );
}

function ChildComponent() {
    const contextValue = useContext(MyContext);

    return <div>{contextValue}</div>;
}

Enter fullscreen mode Exit fullscreen mode

C. codeExample of how the useContext Hook can be used to access and update context in a real-world project:

  • In this example, we have a ThemeContext that contains the current theme (light or dark) and a function to toggle the theme.
  • The App component provides the context and the Header and Body components consume the context and use it to change the background color based on the theme.
import { createContext, useContext } from 'react';

const ThemeContext = createContext();

function App() {
    const [theme, setTheme] = useState("light");

    const toggleTheme = () => {
        setTheme(theme === "light" ? "dark" : "light");
    }

    return (
        <ThemeContext.Provider value={{ theme, toggleTheme }}>
            <Header />
            <Body />
        </ThemeContext.Provider>
    );
}

function Header() {
    const { theme, toggleTheme } = useContext(ThemeContext);

    return (
        <header style={{ backgroundColor: theme === "light" ? "white" : "black" }}>
            <button onClick={toggleTheme}>Toggle Theme</button>
        </header>
    );
}

function Body() {
    const { theme } = useContext(ThemeContext);

    return (
        <div style={{ backgroundColor: theme === "light" ? "white" : "black" }}>
            {/* content */}
        </div>
    );
}

Enter fullscreen mode Exit fullscreen mode

D. Tips and best practices for using the useContext Hook:

  • Always use the createContext function to create a new context, rather than passing a plain object or a custom object.
  • Use the Provider component to provide the context, and avoid using multiple providers for the same context.
  • Be careful when using the context in large, complex components or in many nested components. It can make your code harder to understand

4.useRef Hook

A. Explanation of what the useRef hook is and what it does

  • The useRef hook allows you to create a reference to a DOM element or a value in a functional component
  • This reference can be used to access and update the value or the element throughout the lifecycle of the component
  • The useRef hook returns a ref object, which you can assign to a variable, and then use that variable to refer to the value or the element

B. How to use the useRef hook in a functional component in code

  • To use the useRef hook, you first need to import it from React: import { useRef } from 'react'
  • Then you can use the hook in your component's body: const myRef = useRef(initialValue)
  • The initialValue is optional, you can pass any type of value or even a DOM element.
  • Once you've created the ref, you can assign it to a variable, and use that variable to refer to the value or the element throughout your component.

C. Example of code how the useRef hook can be used to create a reference to a DOM element or a value in a real-world project

import { useRef } from 'react'

function MyComponent() {
    const inputRef = useRef(null);

    function handleClick() {
        inputRef.current.focus();
    }

    return (
        <>
            <input ref={inputRef} type="text" />
            <button onClick={handleClick}>Focus Input</button>
        </>
    )
}

Enter fullscreen mode Exit fullscreen mode

In this example, we used useRef to create a reference to an input element and we used that reference to focus the input element when the button is clicked.

D. Tips and best practices for using the useRef hook

  • useRef does not cause re-rendering, so it is efficient to use when you don't want to cause unnecessary re-renders.

  • Be careful when using the useRef hook with objects or arrays. Assigning a new object or array to the ref will not trigger a re-render, so the component will not update.

  • Do not use the useRef hook for state that should trigger a re-render, useState hook should be used instead.

  • It's best practice to use useRef only when you need to access a DOM element or an instance of a class component.

Additional resources for learning more about React hooks

Final thoughts on the importance of hooks in modern React development

  • Hooks allows you to write functional components with state and side effects
  • Hooks enables you to easily reuse logic across components Hooks help you to avoid unnecessary re-renders and improve the performance of your application
  • Overall, hooks are a powerful tool that can help you write more efficient and maintainable code in your React projects.

Top comments (0)