UseContext hook provides a way to pass data through the component tree without having to pass props down manually at every level. It helps maintaining cleaner code. In other words – it provides a way to share values between components without having to explicitly pass a prop through every level of the tree.
UseContext hook is useful to share data that can be considered “global” for a tree of React components. Lets create one:
In the example above we created new context (createContext function) and wrapped returned data from DataContextProvider component in Provider component, which allows consuming components to subscribe to context changes. In other words – all components that are wrapped by Provider component has access to data in context (in this case: data variable and setData function).
Lets add same more code: swithData function and DataContextProvider export, and whole dataContext component will be look like this:
When we created context component using useContext hook, we must wrapped all components, that may use data from context. That’s why we add to our App.js component this code:
App.js:
import React from 'react';
import HookExample from './components/hookExample.js';
import DataContextProvider from './contexts/dataContext.js';
function App() {
return (
<DataContextProvider >
<HookExample />
</DataContextProvider>
);
}
export default App;
Now we can use our context data in other components:
HookExample.js:
import React, { useContext } from 'react';
import { DataContext } from '../contexts/dataContext.js';
const HookExample = () => {
const dataContext = useContext(DataContext);
return (
<div className="App">
<p style={{color: dataContext.data.color}}>This is sample text</p>
<div style={{display: dataContext.data.isTextVisible ? "block" : "none"}}>
This is more text, which is visible if clicked on "Show more text" button
</div>
<div>
<button onClick={() => dataContext.setData({type: "changeColor", color: 'red'})}>Change text color</button>
<button onClick={() => dataContext.setData({type: "changeTextVisibility"})}>Show more text</button>
</div>
</div>
);
}
export default HookExample;
In HookExample component we used data (data.color, data.isTextVisible variables and setData function) taken from dataContext component, where we created context. In the same way we can use this data in other components, if they are wrapped by DataContextProvider component (which returns our context).
I write code that (usually) works. I tame WordPress, learn React, explore the world of DevOps, and click around in the Linux console. I optimize code as a hobby, because laziness is the purest form of productivity. I do my best not to lose my mind somewhere between a bug and a deadline.