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.