To update a React Context from inside a child component, we can wrap the React Context provider around the child components. Then we set the value prop of the context provider to the the state setter function that lets us update the context value. Then we can use the useContext hook to access the context.
React hooks are introduced in React 16.8. If you are familiar with the class components then there is no difference to change the parent component state from child component. In both cases, you have to pass the callback function to the parent.
To pass data from child to parent component in React:Pass a function as a prop to the Child component. Call the function in the Child component and pass the data as arguments. Access the data in the function in the Parent .
Forcing an update on a React class component In any user or system event, you can call the method this. forceUpdate() , which will cause render() to be called on the component, skipping shouldComponentUpdate() , and thus, forcing React to re-evaluate the Virtual DOM and DOM state.
Hooks were introduced in 16.8.0 so the following code requires a minimum version of 16.8.0 (scroll down for the class components example). CodeSandbox Demo
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 [language, setLanguage] = useState("en");
const value = { language, setLanguage };
return (
...
);
};
The language
is stored in the state. We will pass both language
and the setter function setLanguage
via context later.
Next, I created a language context like this:
// set the defaults
const LanguageContext = React.createContext({
language: "en",
setLanguage: () => {}
});
Here I'm setting the defaults for language
('en') and a setLanguage
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
.
Note: the LanguageContext
remains same whether you use hooks or class based components.
In order to have the language switcher set the language, it should have the access to the language setter function via context. It can look something like this:
const LanguageSwitcher = () => {
const { language, setLanguage } = useContext(LanguageContext);
return (
<button onClick={() => setLanguage("jp")}>
Switch Language (Current: {language})
</button>
);
};
Here I'm just setting the language to 'jp' but you may have your own logic to set languages for this.
Now I'll render my language switcher component in a LanguageContext.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 [language, setLanguage] = useState("en");
const value = { language, setLanguage };
return (
<LanguageContext.Provider value={value}>
<h2>Current Language: {language}</h2>
<p>Click button to change to jp</p>
<div>
{/* Can be nested */}
<LanguageSwitcher />
</div>
</LanguageContext.Provider>
);
};
Now, whenever the language switcher is clicked it updates the context dynamically.
CodeSandbox Demo
The latest context API was introduced in React 16.3 which provides a great way of having a dynamic context. The following code requires a minimum version of 16.3.0. CodeSandbox Demo
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:
class App extends Component {
setLanguage = language => {
this.setState({ language });
};
state = {
language: "en",
setLanguage: this.setLanguage
};
...
}
The language
is stored in the state along with a language setter method, which you may keep outside the state tree.
Next, I created a language context like this:
// set the defaults
const LanguageContext = React.createContext({
language: "en",
setLanguage: () => {}
});
Here I'm setting the defaults for language
('en') and a setLanguage
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
.
In order to have the language switcher set the language, it should have the access to the language setter function via context. It can look something like this:
class LanguageSwitcher extends Component {
render() {
return (
<LanguageContext.Consumer>
{({ language, setLanguage }) => (
<button onClick={() => setLanguage("jp")}>
Switch Language (Current: {language})
</button>
)}
</LanguageContext.Consumer>
);
}
}
Here I'm just setting the language to 'jp' but you may have your own logic to set languages for this.
Now I'll render my language switcher component in a LanguageContext.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:
class App extends Component {
setLanguage = language => {
this.setState({ language });
};
state = {
language: "en",
setLanguage: this.setLanguage
};
render() {
return (
<LanguageContext.Provider value={this.state}>
<h2>Current Language: {this.state.language}</h2>
<p>Click button to change to jp</p>
<div>
{/* Can be nested */}
<LanguageSwitcher />
</div>
</LanguageContext.Provider>
);
}
}
Now, whenever the language switcher is clicked it updates the context dynamically.
CodeSandbox Demo
Since it is recommended by React to use functional components and hooks so I will implement it with useContext and useState hooks. Here is how you can update the context from within a child component.
LanguageContextMangement.js
import React, { useState } from 'react'
export const LanguageContext = React.createContext({
language: "en",
setLanguage: () => {}
})
export const LanguageContextProvider = (props) => {
const setLanguage = (language) => {
setState({...state, language: language})
}
const initState = {
language: "en",
setLanguage: setLanguage
}
const [state, setState] = useState(initState)
return (
<LanguageContext.Provider value={state}>
{props.children}
</LanguageContext.Provider>
)
}
App.js
import React, { useContext } from 'react'
import { LanguageContextProvider, LanguageContext } from './LanguageContextManagement'
function App() {
const state = useContext(LanguageContext)
return (
<LanguageContextProvider>
<button onClick={() => state.setLanguage('pk')}>
Current Language is: {state.language}
</button>
</LanguageContextProvider>
)
}
export default App
One quite simple solution is to set state on your context by including a setState method in your provider like so:
return (
<Context.Provider value={{
state: this.state,
updateLanguage: (returnVal) => {
this.setState({
language: returnVal
})
}
}}>
{this.props.children}
</Context.Provider>
)
And in your consumer, call updateLanguage like so:
// button that sets language config
<Context.Consumer>
{(context) =>
<button onClick={context.updateLanguage({language})}>
Set to {language} // if you have a dynamic val for language
</button>
<Context.Consumer>
I personally like this pattern:
File: context.jsx
import React from 'react';
// The Context
const TemplateContext = React.createContext({});
// Template Provider
const TemplateProvider = ({children}) => {
const [myValue, setMyValue] = React.useState(0);
// Context values passed to consumer
const value = {
myValue, // <------ Expose Value to Consumer
setMyValue // <------ Expose Setter to Consumer
};
return (
<TemplateContext.Provider value={value}>
{children}
</TemplateContext.Provider>
)
}
// Template Consumer
const TemplateConsumer = ({children}) => {
return (
<TemplateContext.Consumer>
{(context) => {
if (context === undefined) {
throw new Error('TemplateConsumer must be used within TemplateProvider');
}
return children(context)
}}
</TemplateContext.Consumer>
)
}
// useTemplate Hook
const useTemplate = () => {
const context = React.useContext(TemplateContext);
if(context === undefined)
throw new Error('useTemplate must be used within TemplateProvider');
return context;
}
export {
TemplateProvider,
TemplateConsumer,
useTemplate
}
Then you can create a functional component, if it is a child in the tree of the provider:
File: component.jsx
import React from 'react';
import {useTemplate} from 'context.jsx';
const MyComponent = () => {
// Get the value and setter from the consumer hook
const {myValue,setMyValue} = useTemplate();
// Demonstrate incrementing the value
React.useEffect(()=>{
let interval = setInterval(()=>{
setMyValue(prev => prev + 1); // Increment, set in context
}, 1000) // Every second
return (
clearInterval(interval); // Kill interval when unmounted
)
},[]) // On mount, no dependencies
// Render the value as it is pulled from the context
return (
<React.Fragment>
Value of MyValue is: {myValue}
</React.Fragment>
)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With