Using React Context
Posted on 7th May 2020
6 min read
While developing a React app you might come across a situation where you need to share a value or state between components. If the state needs to be shared between two components and they have a direct parent-child relationship, we can pass the state from the parent to the child through props. Or if we want to share the state within multiple components we might look into something like Redux.
If the state to be shared isn’t complex, Redux might be overkill as it takes some effort to setup and use. For those cases we can use React Context.
What we will be building
We will build an app that fetches and displays users from JSONPlaceholder.
The app will be divided into three parts.
- Context - Used to share the state within the app.
- Controls - The component used to change the user.
- Display - The component used to display the User data.
The Context
The Context will share any value given to it to its direct descendants.
In our case, we will need to share four sets of data.
userId
- The state that holds the current User ID.setUserId
- The function that updates theuserId
state.user
- The state the holds the User data.isFetching
- The state that will be used to indicate if the app is currently in the middle of fetching a user so that the controls can be disabled.
To start, create the context
folder and in it create the UserContext.js
file.
src/context/UserContext.js
Next let’s create and export the context with some default values.
src/context/UserContext.js
After that we’ll declare a Context Provider which will expose the context to its child components.
src/context/UserContext.js
Then let’s declare the userId
, user
and isFetching
states and pass them to the provider.
src/context/UserContext.js
Now we’ll setup an effect to automatically update the user
state whenever the userId
state is changed.
src/context/UserContext.js
The Display component
Next let’s create the Display
component. Create the components
folder and in it add the file Display.js
.
src/components/Display.js
Now we can get the user
state by from the UserContext
with the useContext
hook.
src/components/Display.js
To finish off the Display
component, let’s display the User data in a table.
src/components/Display.js
The Controls component
The Controls
component is used to change the current userId
.
To start create the Controls.js
file in the components
folder.
src/components/Controls.js
After that, we can get userId
, setUserId
and isFetching
from UserContext
.
src/components/Controls.js
Next we can add two buttons to change the userId
.
src/components/Controls.js
Finally we will add a check to the buttons to disable them if the app is already fetching a user or to stop userId
from being set to value less than 1 or more than 10.
src/components/Controls.js
Bringing it all together
Now all that’s left to is to bring everything together in the root component.
src/App.js
Then we should wrap the root div
in the UserProvider
to make the context available to all components.
src/App.js
Finally add the Display
and Controls
components.
src/App.js
Wrapping up
Below is a sample of the app we just built. If you think you missed something, feel free to check out the code.
If you found this post helpful please be sure to share it! 😊
0 views
Leave a Reaction below!!! (No login required)