DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. React.js Context Tutorial: A Better Way to Store State?

React.js Context Tutorial: A Better Way to Store State?

We look in to the JavaScript code necessary to allow your React.js application to store state within the Context layer of its architecture.

Piyush Arora user avatar by
Piyush Arora
·
Mar. 12, 19 · Tutorial
Like (3)
Save
Tweet
Share
24.07K Views

Join the DZone community and get the full member experience.

Join For Free

While creating a component in a typical React app, we end up creating layers of functional components and class components. Much of the time, we end up following a pattern where we pass props to a functional or class component from Grand Parents to Great Grand Children (referring to the hierarchy shown below). There are cases when props are not required at a certain level but are there just to be passed to another component.

Image title

We can use React's context pattern to solve this problem. It's based on the Consumer Provider pattern, i.e. a producer stores the state and methods to manipulate the state and the consumer can access them whenever necessary. 

Image title

Demo: Create a React app with create-react-app and remove the boilerplate code.

Now use the ceateContext as shown below (I've named this file provider.js ):

import React from 'react'

export const Context = React.createContext();
class ContextProvider extends React.Component {

state={digit:2}

    render() {
        return (<Context.Provider value={{digit:this.state.digit}} >
            {this.props.children}
        </Context.Provider>)
    }
}
export default ContextProvider

Here we created a Context that is provided by the React.createContext API. We then sore thestate at this level, and provide its schemas as  {digit:2}. Then we simply return Context.Provider with props as the value.

Now we modify the index.js file by wrapping the App component in the <Context.Provider> component.

ReactDOM.render(
<ContextProvider>
   <App />
</ContextProvider> , document.getElementById('root'));

Now it is possible to access the state maintained by Context at any level inside the App component without passing it down.

I created a GreatGrandChildren.js file that looks like the below code block:

import React from 'react'
import { Context } from './ContextProvider'
export const GreatGrandChildren = () => {
    return (<React.Fragment>
        <br></br>
        <Context.Consumer>
            {(context) => <span>   GreatGrandChildren {context.digit}</span>}
        </Context.Consumer>

    </React.Fragment>)
}

Here we need to import the Context that we created and render our component as a function inside <Context.Consumer>.

We can now access Context values inside the Consumer at any level. This saves us from performing prop chaining.

Now, if we want to update the state, add a handler method in context, and make it so it can be accessed by the consumer to handle the state value, we need to modify the context provider's code to add the handler.

 return (<Context.Provider value={{ digit: this.state.digit, onDigitChange: () =>
         this.setState({ digit: this.state.digit + 1 })
          }} >

 And we can access this handler from the consumer by doing the following:

 <Context.Consumer>
            {(context) => <span>   GreatGrandChildren {context.digit}  <button onClick={context.onDigitChange}>increase digit</button></span>}
 </Context.Consumer>

Get the code on GitHub.



If you enjoyed this article and want to learn more about React, check out this collection of tutorials and articles on all things React.

React (JavaScript library) consumer app Schema Pass (software) Blocks GitHub IT

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Public Key and Private Key Pairs: Know the Technical Difference
  • How To Use Java Event Listeners in Selenium WebDriver
  • Automated Testing With Jasmine Framework and Selenium
  • How To Create a Failover Client Using the Hazelcast Viridian Serverless

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: