DEV Community

Cover image for Differences between hook and context in react
Ricardo Maia
Ricardo Maia

Posted on

Differences between hook and context in react

In React, both Hooks and Context are powerful tools for managing states and sharing information between components, but they serve different purposes. Letโ€™s understand the difference between Hooks and Context:

  1. ๐—›๐—ผ๐—ผ๐—ธ๐˜€: Hooks were introduced in React 16.8 and allow you to use state and other React features without needing to write class components. They are functions that let you "hook" functionality into functional components.

๐—ง๐˜†๐—ฝ๐—ฒ๐˜€ ๐—ผ๐—ณ ๐—›๐—ผ๐—ผ๐—ธ๐˜€:

  • ๐™ช๐™จ๐™š๐™Ž๐™ฉ๐™–๐™ฉ๐™š: Manages the local state of a functional component.
  • ๐™ช๐™จ๐™š๐™€๐™›๐™›๐™š๐™˜๐™ฉ: Allows you to perform side effects, such as API calls, after the component renders.
  • ๐™ช๐™จ๐™š๐™ˆ๐™š๐™ข๐™ค ๐™–๐™ฃ๐™™ ๐™ช๐™จ๐™š๐˜พ๐™–๐™ก๐™ก๐™—๐™–๐™˜๐™ : Optimize performance by memoizing values or functions.
  • ๐™ช๐™จ๐™š๐™๐™š๐™™๐™ช๐™˜๐™š๐™ง: An alternative to useState for managing more complex states.
  • ๐™ช๐™จ๐™š๐™๐™š๐™›: Directly accesses DOM elements or persists values between renders without causing re-renders.

Example of useState:

Image description

  1. ๐—–๐—ผ๐—ป๐˜๐—ฒ๐˜…๐˜: The React Context API is a way to share data globally between components without needing to manually pass "props" through each level of the component tree. It allows you to "inject" information anywhere in a child component, avoiding "props drilling" (passing props from one component to another in a chain).
  • Context is useful for sharing states or information used by multiple components, such as authentication data, application theme (light/dark mode), language, etc.

Example of Context:
First, we create a context and provide a value:

Image description

๐—ž๐—ฒ๐˜† ๐——๐—ถ๐—ณ๐—ณ๐—ฒ๐—ฟ๐—ฒ๐—ป๐—ฐ๐—ฒ๐˜€:

  1. ๐—ฃ๐˜‚๐—ฟ๐—ฝ๐—ผ๐˜€๐—ฒ:

    • ๐—›๐—ผ๐—ผ๐—ธ๐˜€: Functions that allow you to use state, lifecycle, and other features in functional components. They solve problems like state management, performance optimization, and side effects.
    • ๐—–๐—ผ๐—ป๐˜๐—ฒ๐˜…๐˜: An API for sharing data between components without needing to pass props through multiple levels. Itโ€™s ideal for global state management, such as theme, authentication, settings, etc.
  2. ๐—จ๐˜€๐—ฎ๐—ด๐—ฒ:

    • ๐—›๐—ผ๐—ผ๐—ธ๐˜€: Used to manipulate the state and behavior of an individual component.
    • ๐—–๐—ผ๐—ป๐˜๐—ฒ๐˜…๐˜: Used to share data or state between multiple components without manually passing props through them.
  3. ๐—ฆ๐—ฐ๐—ผ๐—ฝ๐—ฒ:

    • ๐—›๐—ผ๐—ผ๐—ธ๐˜€: Apply to the scope of a single component or a set of components sharing logic.
    • ๐—–๐—ผ๐—ป๐˜๐—ฒ๐˜…๐˜: Operates in the global (or broader) scope to provide data to many components without props drilling.
  4. ๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐—ฎ๐—ฐ๐˜๐—ถ๐—ผ๐—ป:

    • You can use useContext (a Hook) to access Context data, combining both concepts. In other words, a Hook can consume data from Context, but they are not the same.

๐—–๐—ผ๐—ป๐—ฐ๐—น๐˜‚๐˜€๐—ถ๐—ผ๐—ป:

  • ๐—›๐—ผ๐—ผ๐—ธ๐˜€ are for managing local states and functionalities in functional components.
  • ๐—–๐—ผ๐—ป๐˜๐—ฒ๐˜…๐˜ is for sharing states or data between different components, usually in a deeper tree, without needing to pass props between them.

Both can be used together: for example, you can use useContext to consume a context in a functional component, combining the benefits of both.

Top comments (1)

Collapse
 
rojanrai_42 profile image
Rojan Rai

In React, both Hooks and Context are powerful tools for managing states

Hi. A small correction here. Not all hooks in react manage state. Good article.