🌐
React
react.dev › reference › react › useContext
useContext – React
Declare a state variable in the parent component, and pass the current state down as the context value to the provider.
🌐
React
legacy.reactjs.org › docs › context.html
Context – React
Context is designed to share data that can be considered “global” for a tree of React components, such as the current authenticated user, theme, or preferred language.
🌐
Kent C. Dodds
kentcdodds.com › blog › how-to-use-react-context-effectively
How to use React Context effectively
You shouldn't be reaching for context to solve every state sharing problem that crosses your desk. Context does NOT have to be global to the whole app, but can be applied to one part of your tree
🌐
React
react.dev › learn › passing-data-deeply-with-context
Passing Data Deeply with Context – React
It is common to use a reducer together with context to manage complex state and pass it down to distant components without too much hassle. Context is not limited to static values. If you pass a different value on the next render, React will update all the components reading it below!
🌐
DEV Community
dev.to › mikevarenek › react-context-for-state-management-167
React Context for State Management - DEV Community
March 2, 2024 - React Context provides a way to create a global state management system within your React application. It allows you to establish a central store for data that can be accessed by any component throughout the component tree.
🌐
Medium
medium.com › @__davidflanagan › react-hooks-context-state-and-effects-aa899d8c8014
React Hooks: context, state and effects | by David Flanagan | Medium
April 1, 2019 - React’s context mechanism is a way to make important data available to any component in the rendered tree without having to pass that data from parent to child down through the entire tree as a property.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-share-state-across-react-components-with-context
How To Share State Across React Components with Context | DigitalOcean
July 22, 2020 - In this tutorial, you’ll share state across multiple components using React context. React context is an interface for sharing information with other components without explicitly passing the data as props. This means that you can share information between a parent component and a deeply ...
Find elsewhere
Top answer
1 of 2
21

You need to export your UserContext, so it can be imported in the components that need it:

export const UserContext = React.createContext();

function App() {
  const [name, setName] = useState('Name');

  return (
    <UserContext.Provider value={{ name, setName }}>
      <Home />
    </UserContext.Provider>
  );
}

Afterwards you can import it in your App component:

import { UserContext } '../../App'

function Home() {
    const user = useContext(UserContext);

    return (
        <>
            <label>Your name:</label>
            <input type='text' onChange={e => user.setName(e.target.value)} />
            <p>{user.name}</p>
        </>
    )
}
2 of 2
9

1. Setting parent state for dynamic context

Firstly, in order to have a dynamic context which can be passed to the consumers, I'll use the parent's state. This ensures that I've a single source of truth going forth. For example, my parent App will look like this:

const App = () => {
  const [name, setName] = useState("John");
  const value = { name, setName };

  return (
   ...
  );
};

The name is stored in the state. We will pass both name and the setter function setName via context later.

2. Creating a context

Next, I created a name context like this:

// set the defaults
const NameContext = React.createContext({
  name: "John",
  setName: () => {}
});

Here I'm setting the defaults for name ('John') and a setName function which will be sent by the context provider to the consumer(s). These are only defaults and I'll provide their values when using the provider component in the parent App.

3. Creating a context consumer

In order to have the name switcher set the name and also showing, it should have the access to the name setter function via context. It can look something like this:

const NameSwitcher = () => {
const { name, setName } = useContext(NameContext);
 return (
    <label>Your name:</label><br />
    <input type='text' onChange={e => setName(e.target.value)} />
    <p>{name}</p>
  );
};

Here I'm just setting the name to input value but you may have your own logic to set name for this.

4. Wrapping the consumer in a provider

Now I'll render my name switcher component in a NameContext.Provider and pass in the values which have to be sent via context to any level deeper. Here's how my parent App look like:

const App = () => {
   const [name, setName] = useState("John");
   const value = { name, setName };

   return (
    <Name.Provider value={value}>
      <NameSwitcher />
    </Name.Provider>
   );
};
🌐
DEV Community
dev.to › hey_yogini › usecontext-for-better-state-management-51hi
useContext for better state management! - DEV Community
January 5, 2022 - To create a context in any React ... The best way to understand context is, consider it as a simple state, a state which we create using useState....
🌐
React
react.dev › reference › react › createContext
createContext – React
To make context change over time, add state and wrap components in a context provider.
🌐
DEV Community
dev.to › olenadrugalya › managing-state-with-react-context-4h2h
Managing state with React Context - DEV Community
July 19, 2021 - Every Context object comes with a Provider React component that allows consuming components to subscribe to context changes. It provides a value prop which will be passed to the components who will need access to Context and state.
🌐
Devtrium
devtrium.com › posts › how-use-react-context-pro
How to use React Context like a pro - Devtrium
September 13, 2021 - In situations where you care about this (and only in those), you can separate your state and your state setters in two different contexts. I believe this idea was first introduced by Kent C. Dodds in this blog post. The implementation of that pattern is the following: ... import React, { createContext, useContext, useState, useEffect, useCallback } from "react"; // create contexts
🌐
freeCodeCamp
freecodecamp.org › news › react-context-for-beginners
React Context for Beginners – The Complete Guide (2021)
July 21, 2021 - Data should be placed on React context that does not need to be updated often. Why? Because context was not made as an entire state management system.
🌐
freeCodeCamp
freecodecamp.org › news › state-management-in-react-props-vs-context-api
State Management in React –Props vs the Context API
May 19, 2023 - The context provider is responsible for creating and managing the global state. It wraps a section of your component tree and makes the state available to all the components within that tree.
🌐
DEV Community
dev.to › imevanc › react-state-vs-react-context-deciding-how-to-manage-state-15e3
React State vs. React Context: Deciding How to Manage State - DEV Community
August 9, 2023 - It's useful when you want to share data globally, like user authentication states or themes. Context lets you provide data to components without manually passing props through every intermediary component. However, it might be overly complex for smaller projects. ... To decide between React state and React Context, consider the scale of your project and how components need to share data.
🌐
vimtutor
remarkablemark.org › blog › 2021 › 03 › 21 › managing-react-state-with-context
Managing React state with Context | remarkablemark
July 24, 2021 - Pass state and dispatch from useReducer to Provider prop value: // Provider.js import { createContext, useReducer } from 'react'; import reducer from './reducer'; export const Context = createContext(); const initialState = { count: 0, }; export default function Provider(props) { const [state, dispatch] = useReducer(reducer, initialState); return ( <Context.Provider value={[state, dispatch]}> {props.children} </Context.Provider> ); }
🌐
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 - Context helps by lifting the state to a parent component, making it accessible to any component that needs it. Often, developers pair Context with a reducer to manage complex state logic, which simplifies the code and makes the app maintainable ...
🌐
freeCodeCamp
freecodecamp.org › news › state-management-with-react-hooks
How to manage state in a React app with just Context and Hooks
September 9, 2019 - Remember that the useReducer hook takes two parameters, a reducer (which is simply a function that takes in state and action as parameters and returns a new state based on an action) and an initial state which will be passed into the reducer. Let's then add the hook into our App component as shown below: import React from "react"; import "./App.css"; import Login from "./components/Login"; import Home from "./components/Home"; import Header from "./components/Header"; export const AuthContext = React.createContext(); const initialState = { isAuthenticated: false, user: null, token: null, }; co
🌐
Toptal
toptal.com › react › react-context-api
How to Work With the React Context API | Toptal®
January 16, 2026 - A React component can have its own state, which holds information about the component’s current configuration.