🌐
W3Schools
w3schools.com › react › react_usecontext.asp
React useContext Hook
In order to use the Context in a child component, we need to access it using the useContext Hook.
🌐
React
react.dev › reference › react › useContext
useContext – React
HooksCopy pageCopy · useContext is a React Hook that lets you read and subscribe to context from your component. const value = useContext(SomeContext) Reference · useContext(SomeContext) Usage · Passing data deeply into the tree · Updating ...
🌐
Jimmydc
blog.jimmydc.com › react-context-hooks
A simple example of the React useContext hook
JS - Snippet Good! now has React Hook snippets! ... I just released version 1.5.0 of my VS Code extension: JS - Snippet Good! ... A bundle visualizer can be a very handy tool for tracking down packages, or even your own code, for large file size offenders ... A complete code example of how to use the useContext ...
🌐
Dave Ceddia
daveceddia.com › usecontext-hook
React useContext Hook Tutorial (with Examples)
October 22, 2020 - React’s useContext hook makes it easy to pass data throughout your app without manually passing props down the tree.
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › reactjs-usecontext-hook
ReactJS useContext Hook - GeeksforGeeks
The useContext hook allows to consume values from a React Context, enabling easy access to shared state across multiple components without prop drilling.
Published   1 week ago
🌐
Medium
medium.com › zestgeek › mastering-reacts-usecontext-hook-simplifying-state-management-65894e6dc431
Mastering React’s useContext Hook: Simplifying State Management | by Love Trivedi | ZestGeek | Medium
February 28, 2024 - The useContext hook is a part of React’s hooks API introduced in React 16.8. It allows components to consume state or context without the need for prop drilling, which can lead to cleaner and more maintainable code.
🌐
Design+Code
designcode.io › react-hooks-handbook-usecontext-hook
useContext Hook - React Hooks Handbook - Design+Code
useState is a great hook to use when we want to manage local states. However, as our application grows, we might want to share states across components or even throughout the entire application. That's when the useContext hook comes in handy.
🌐
freeCodeCamp
freecodecamp.org › news › react-hooks-useeffect-usestate-and-usecontext
How to Use React Hooks – useEffect, useState, and useContext Code Examples
December 4, 2023 - The useContext hook is used to consume values from a React context. Context provides a way to pass data through the component tree without having to pass props manually at every level.
🌐
Pragimtech
pragimtech.com › blog › reactjs › usecontext-hook-in-react
useContext Hook in React
import ReactDOM from "react-dom"; import React, { Component, useState, useContext } from "react"; const employeeContext=React.createContext(); function App(){ const [employee,setEmployee]=useState({Id:101,Name:'Pragim', Location:'Bangalore',Salary:12345}); return( <div> <h2>Welcome to App Component...</h2> <employeeContext.Provider value={employee}> <Employee></Employee> </employeeContext.Provider> </div> ); } function Employee(){ let context=useContext(employeeContext); return( <div> <h2>Welcome to Employee Component...</h2> <p> <label>Employee ID : <b>{context.Id}</b></label> </p> <p> <label
Find elsewhere
🌐
Dmitri Pavlutin
dmitripavlutin.com › react-context-and-usecontext
A Guide to React Context and useContext() Hook
February 2, 2023 - The hook returns the value of the context: value = useContext(Context).
🌐
Telerik
telerik.com › blogs › react-usecontext-hook
The React useContext Hook
March 21, 2023 - The useContext Hook provides function components access to the context value for a context object.
🌐
Robin Wieruch
robinwieruch.de › react-usecontext-hook
How to useContext in React - Robin Wieruch
June 27, 2021 - React’s useContext Hook takes the Context as parameter to retrieve the value from it.
🌐
DigitalOcean
digitalocean.com › community › tutorials › react-usecontext
How To Work with Context API in React and React Hooks | DigitalOcean
November 12, 2020 - Compared to the Context API, the ... to avoid prop-drilling in your component tree. The React Hook useContext() applies the same functionality in a streamlined, functional component body in one call....
Top answer
1 of 4
8

You should understand it first that, useContext is just to make use of Context and acts like a consumer and not Provider.

To answer your questions

Do I need to use HOC or it's a new way to do this?

You don't need an HOC with hooks. Hooks are meant to replace HOCs and render props pattern.

Do I need the Context.Provider or it's new Hook?

There is no hooks equivalent of Context.Provider. You have to use it as is.

Do I need to declare default value as a null or I can pass my Object right from context.js

The default value to createContext is only used if you don't pass a value props to the Context.Provider. If you pass it the default value is ignored.

How can I use a new Hook instead of HOC in mine code?

Instead of using useContext in the component returned by HOC use it directly within the component

Sample code

/ context.js this is my hoc
// index.jsx
import App from './App'
import Firebase, { FirebaseContext } from './components/Firebase'

const FirebaseContext = React.createContext(null)

ReactDOM.render(
  <FirebaseContext.Provider value={new Firebase()}>
    <App />
  </FirebaseContext.Provider>,
  document.getElementById('root'),
)

App.jsx

const App = () => {
    const firebase = useContext(FirebaseContext) 
    return(...)
}
export default App;
2 of 4
0
  1. Do I need to use HOC or it's a new way to do this?

No, you don't need to use HOC as best technique.

Why? Starting from React v7.0, you can use functional-based components. From this version efficient is to use the the latest technique named HOOKS, which were designed to replace class and provide another great alternative to compose behavior into your components.


  1. Do I need the Context.Provider or it's new Hook?

Hook like useContext() has a relation with Context.Provider.
Context is designed to share data that can be considered “global”.

The Provider component accepts a value prop to be passed. Every Context come with a Provider.

Context.Provider component available on the context instance is used to provide the context to its child components, no matter how deep they are.


  1. Do I need to declare default value as a null or I can pass my Object right from context.js?

No, you don't need necessarily to declare a default value.

Example of defining the context in one corner of the codebase without defaultValue.

const CountStateContext = React.createContext() // <-- define the context without defaultValue


  1. How can I use a new Hook instead of HOC in mine code?

index.jsx

import App from './App'

import Firebase, { FirebaseContext } from './components/Firebase'

const FirebaseContext = React.createContext(null)

ReactDOM.render(
  <FirebaseContext.Provider value={new Firebase()}>
    <App />
  </FirebaseContext.Provider>,
  document.getElementById('root'),
)

Root Component: App.js, where will be used data comes form context:

const App = () => {
    const firebase = useContext(FirebaseContext) 
    return(...)
}
export default App;
🌐
React
legacy.reactjs.org › docs › hooks-reference.html
Hooks API Reference – React
Read the new React documentation for useContext. ... Accepts a context object (the value returned from React.createContext) and returns the current context value for that context. The current context value is determined by the value prop of the nearest <MyContext.Provider> above the calling component in the tree. When the nearest <MyContext.Provider> above the component updates, this Hook will trigger a rerender with the latest context value passed to that MyContext provider.
🌐
Codecademy
codecademy.com › docs › react › hooks › usecontext()
React | Hooks | useContext() | Codecademy
September 5, 2023 - The useContext() hook subscribes a child component to a context which includes its value prop that exists further up the component tree.
🌐
LogRocket
blog.logrocket.com › home › react context tutorial: complete guide with practical examples
React Context tutorial: Complete guide with practical examples - LogRocket Blog
February 19, 2025 - By calling, useContext(MyContext), you get the current value from the nearest <MyContext /> provider above your component in the tree. If no provider is found, the useContext() Hook returns the default value defined when you created MyContext.
🌐
ReScript
rescript-lang.org › docs › react › latest › hooks-context
useContext Hook | ReScript React
// App.res module ThemeContext = { let context = React.createContext("light") module Provider = { let make = React.Context.provider(context) } } module ThemedButton = { @react.component let make = () => { let theme = React.useContext(ThemeContext.context) let (color, backgroundColor) = switch theme { | "dark" => ("#ffffff", "#222222") | "light" | _ => ("#000000", "#eeeeee") } <button style={{color, backgroundColor}}> {React.string("I am a styled button!")} </button> } } module Toolbar = { @react.component let make = () => { <div> <ThemedButton /> </div> } } @react.component let make = () => { <ThemeContext.Provider value="dark"> <div> <Toolbar /> </div> </ThemeContext.Provider> }
🌐
Medium
medium.com › technofunnel › usecontext-in-react-hooks-aa9a60b8a461
How to use “useContext” in React Hooks | by Mayank Gupta | TechnoFunnel | Medium
May 11, 2022 - Technofunnel presents another article ... “useContext” hook is used to create common data that can be accessed throughout the component hierarchy without passing the props down manually to each level....