paint-brush
How to get started with the React Context APIby@spences10
11,555 reads
11,555 reads

How to get started with the React Context API

by Scott SpenceMay 29th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Ok terrible puns aside, let’s have a look at what the React Context API is for and what it does. There’s a great one-liner from the <a href="https://reactjs.org/docs/context.html" target="_blank">React docs</a>…

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How to get started with the React Context API
Scott Spence HackerNoon profile picture

Use the React Context API to change the theme in an app!

But first, some context! 🤣

Ok terrible puns aside, let’s have a look at what the React Context API is for and what it does. There’s a great one-liner from the React docs

Context provides a way to pass data through the component tree without having to pass props down manually at every level.

Or in other words, you can use the React Context API to avoid prop drilling. If you need more detail on the concept then please do check out the links provided.

I’ve previously gone over implementing the React Context API in my Gatsby blog which I documented as I did it. You can see how that went here.

A great resource on explaining the API can be found from @leighchalliday with a great use case on the subject.

What we’re doing…

For this post we’re going to extend the example we created for styled-components getting started. As it has the majority of the code we’ll need to get started with the React Context API.

We’re going to extend that example to manage the theme state of the example application.

So in summary:

  • Scaffold out basic CreateReact App
  • Use styled-components 💅 for styling
  • Add themes to switch between with the React Context API
  • Use the React Context API!

What we’ll need…

All we’ll be needing is an internet connection and a modern web browser! Because we’re going to do all of this online in the awesome CodeSandbox!

If you have a GitHub account or not, CodeSandbox will let you get started

.

Versions:

This guide is being used with the following dependency versions.

  • react: 16.4.2
  • react-dom: 16.4.2
  • react-scripts: 1.1.4
  • styled-components: 3.4.5

Let’s start

So let’s go over theming the basic create-react-app again. This time, instead of adding state into to the component, we will use the React Context API to manage the state for us. There will be people that will argue that this is a bit overkill for a theme switch. It is given as an example of when to use the Context API in the React documentation, so I will let you decide on the validity of that point.

For this example, I hope it will give you a clearer picture of how to use the Context API in an application.

Dependencies

and add styled-components as a dependency:

File structure

Another area for bikeshedding is file structure. In this scenario we’re adding folders for components, contexts and the theme. Please feel free to structure your files how you see fit, this is how we’re going to do it for this example ❤️

Add the directories into the src folder so we can add in some components. The file structure should look something like this:







context-demo/├─ public/├─ src/│ └─ components/│ └─ contexts/│ └─ theme/└─ package.json

Scaffold out a basic Create React App

Ok, so, what we’re going to do is add in an App.js component to the components folder then use that in the src/index.js file.

The App.js component can be a stateless functional component as for this example as we’re going to be handling state with the Context API.

Here you can see my sketchy typing as I create the directories and add in the App.js component.

We can then remove the style.css file and reference in src/index.js. We’re going to be styling with styled-components 💅 and then use our App.js component:

Ok, so the reason why I have abstracted the App.js component out of the src/index.js file is so that when we come to using the Context API we can add it to the highest level in our app, which is src/index.js.

What about the rest?

So this isn’t really the Create React App, as we’re using CodeSandbox instead. I have gone over the basic styling used in the styled-components getting started post. It’s time to refer to that to mimic the styles we need.

That means that, rather than going into depth on the styling of each of the component parts that make up the basic Create React App appearance, we’re going to re-use components. There’s going to be a bit of copy pasting involved now.

The Create React App boilerplate code has one file that we go over styling in the styled-components getting started post which is the App.js file. The basic style of App.js is:

**App.css**



.App {text-align: center;}




.App-logo {animation: App-logo-spin infinite 20s linear;height: 80px;}






.App-header {background-color: #222;height: 150px;padding: 20px;color: white;}



.App-title {font-size: 1.5em;}



.App-intro {font-size: large;}








@keyframes App-logo-spin {from {transform: rotate(0deg);}to {transform: rotate(360deg);}}

Use styled components for styling

Now we’re going to recreate the styles from the App.css file with styled-components. Let’s list them out here and go through them:





AppWrapperAppHeaderAppTitlerotate360AppLogo

_We’re adding our own styles for_AppIntro



UnderlineStyledHyperLinkButton

AppWrapper is the top level wrapper which in a larger component could be used for layout with CSS Grid or Flexbox. In our case we’re going to align the text center.

Straightforward enough, right? Now the majority of the rest of the components will use the styled-components [ThemeProvider](https://www.styled-components.com/docs/advanced#theming) which is what we’re going to pass our theme to from the Context API.

Add themes to switch between with the React Context API

Ok, we need to define some themes to pass to the ThemeProvider. We’re going to define several theme aspects we want to change, and these are going to be:

primary // _colour_secondary // _colour_danger // _colour_fontHeader // _font_fontBody // font

Create a file to contain the theme object in the theme directory and call it globalStyle.js. Add in the following:

import { injectGlobal } from 'styled-components'























export const themes = {theme1: {primary: '#ff0198',secondary: '#01c1d6',danger: '#e50000',fontHeader: 'Old Standard TT, sans, sans-serif',fontBody: 'Nunito, sans-serif'},theme2: {primary: '#6e27c5',secondary: '#ffb617',danger: '#ff1919',fontHeader: 'Enriqueta, sans-serif',fontBody: 'Exo 2, sans, sans-serif'},theme3: {primary: '#f16623',secondary: '#2e2e86',danger: '#cc0000',fontHeader: 'Kaushan Script, sans, sans-serif',fontBody: 'Headland One, sans-serif'}}


injectGlobal`@import url('https://fonts.googleapis.com/css?family=Old+Standard+TT:400,700|Nunito:400,700'|Enriqueta:400,700|Exo+2:400,700|Kaushan+Script:400,700|Headland+One:400,700|');





body {padding: 0;margin: 0;}`

Ok, so nothing really happening there apart from setting up the styles for use later.

You will notice that injectGlobal is being used here. This is where we’re setting the fonts for use throughout the app. ingectGlobal should be used once in an app to set global styles like this.

Onwards! Let us now focus on getting the basic app styles into the App.js component. We can now start using the ThemeProvider in App.js. To do this, for now, to get some visual feedback we’re going to apply one of the themes from the themes object in globalStyle.js. This is so, as we are adding in components we can see the theme being applied.

We can do this now with the AppHeader which is a styled div:






const AppHeader = styled_.div_`height: 12rem;padding: 1rem;color: ${({ theme }) => theme_.dark_};background-color: ${({ theme }) => theme_.primary_}_;_`

You will notice here that we’re beginning to use the styled-components, theme props. If we paste this code in now there won’t be any change until the ThemeProvider is passed the theme object. We’re going to wrap App.js with the ThemeProvider component so that any component encapsulated by the ThemeProvider is able to receive theme props.

AppTitle is going to be a h1 so:



const AppTitle = styled_.h1_`font-family: ${({ theme }) => theme_.fontHeader_}_;_`

For the spinning React logo we can use the asset used previously in the

. We can add it in with the imports at the top of the App.js component and add it into the AppLogo styled component as an img tag:

const logo = 'https://user-images.githubusercontent.com/234708/37256552-32635a02-2554-11e8-8fe3-8ab5bd969d8e.png'

The keyframes helper will need to be imported alongside the ThemeProvider for the animation on the React logo.








const rotate360 = keyframes`_from {transform: rotate(0deg);}to {transform: rotate(360deg);}_`






const AppLogo = styled_.img_`animation: ${rotate360} infinite 5s linear;height: 80px;&:hover {animation: ${rotate360} infinite 1s linear;}`

Shared components

Shared components are covered in the styled-components getting started guide. If you need more information, for this example we’re going to bring in the final couple of components as shared ones for the StyledHyperlink and Button. In src/Shared.js add the following:

**src/Shared.js**
































import styled, { css } from ‘styled-components’export const Button = styled_.button_`padding: 0.5rem 1rem;margin: 0.5rem 1rem;color: ${({ theme }) => theme_.primary_};font-size: 1rem;box-shadow: 0 3px 5px rgba(0, 0, 0, 0.1);cursor: pointer;border: 2px solid ${props => props_.border_};background-color: Transparent;text-transform: uppercase;border-radius: 4px;transition: all 0.1s;&:hover {transform: translateY(1px);box-shadow: 0 2px 3px rgba(0, 0, 0, 0.15);} ${props =>props_.primary_ &&css`background: ${({ theme }) => theme_.primary_};border: 2px solid ${({ theme }) => theme_.primary_};color: white; `}; ${props =>props_.danger_ &&css`background: ${({ theme }) => theme_.danger_};border: 2px solid ${({ theme }) => theme_.danger_};color: white; `}_;&:hover {transform: translateY(2px);box-shadow: 0 2px 3px rgba(0, 0, 0, 0.15);}_`











export const StyledHyperLink = styled_.a_`cursor: pointer;&:visited,&:active {color: ${({ theme }) => theme_.primary_};}&:hover {color: ${({ theme }) => theme_.secondary_};}color: ${({ theme }) => theme_.primary_}_;_`

Then import the components like any other:

The last three components for now, AppIntro, Underline and StyledHyperLink:








const AppIntro = styled_.p_`color: ${({ theme }) => theme_.dark_};font-size: large;code {font-size: 1.3rem;}font-family: ${({ theme }) => theme_.fontBody_}_;_`



const Underline = styled_.span_`border-bottom: 4px solid ${({ theme }) => theme_.secondary_}_;_`





const StyledHyperLink = SHL.extend`text-decoration: none;font-family: ${({ theme }) => theme_.fontBody_};color: ${({ theme }) => theme_.fontDark_}_;_`

Add them in under the AppLogo styled component. Then we can add the rest of the components into the App function return, so, ready for another copy paste? Here:
















































<AppIntro>Bootstrapped with{' '}<Underline><code><StyledHyperLinkhref={`https://github.com/facebook/create-react-app`}target="_blank"rel="noopener">create-react-app</StyledHyperLink></code></Underline>.</AppIntro><AppIntro>Components styled with{' '}<Underline><code><StyledHyperLinkhref={`https://www.styled-components.com`}target="_blank"rel="noopener">styled-components</StyledHyperLink></code></Underline>{' '}<span role="img" aria-label="nail polish">💅</span></AppIntro><AppIntro>Fonts picked with{' '}<Underline><code><StyledHyperLinkhref={`https://fontjoy.com/`}target="_blank"rel="noopener">fontjoy.com</StyledHyperLink></code></Underline></AppIntro><Button>Normal Button</Button><Button primary>Primary Button</Button><Button danger>Danger Button</Button>

Sorry for the code wall! Right paste that in under the closing </AppHeader> tag and we should have the base of what we’re going to theme!

Ok? How’s it looking?

Now we have a basic React app that uses styled-components!

Use the React Context API

Now for the main event! Here we’re going to cover:

  • Making the theme context.
  • Using the context API with a component.
  • Consuming the Context API in multiple components.

So, passing state needlessly through components is what we can use the Context API to avoid. If we take a look at the

we can see the state being managed in the App.js component.

The handleThemeChange function has to be passed to the ThemeSelect component much the same way as any props would need to be passed down. That is a simplified example. It’s quite easy to imagine that if that component lived on a footer component or a menu item, there would be several other components that would need to have the state passed through them that would not actually need that state or props. Make sense?

example








<App> {/* state begins here */}<Header> {/* through here */}<Navigation> {/* and here */}<ThemeSelect> {/* to be used here */}</Navigation></Header><Footer/></App>

Add the site theme context

In our src/contexts/ directory we’re going to make our SiteThemeContext.js import React and define and export our context:

import React from 'react'

export const SiteThemeContext = React_._createContext()

So what is a context?

A context is made up of two things, a provider and a consumer. You have a single provider which will sit up as high as possible in the component tree so that multiple consumers can get the state and props from the provider.

Hopefully you recall the point at which we abstracted the function App component out of the src/index.js file. This is so we could add in the context provider at the highest level of the app, in the src/index.js file. This means that any consumer within the app, no matter how deep into the component tree it is, it can get the state and props from that top level.

Now to create a provider, the provider is a regular React component, so:

import React from 'react'

export const SiteThemeContext = React_._createContext()

export class SiteThemeProvider extends React_._Component {








render() {return (<SiteThemeContext.Provider value={}>{this_.props._children}</SiteThemeContext.Provider>)}}

What is being returned by <SiteThemeProvider> is the <SiteThemeContext.Provider> and the children of that component. The one prop you have to provide the provider is a value prop. This is the variable that the consumer has access to. The consumer being <SiteThemeContext.Consumer> (more on this shortly).

So what we can do now is have what is passed into value be an object value={{}} so it can store multiple properties of the state and the functions that are defined in SiteThemeContt.

The state for the context needs to be the theme. We need to import the theme from src/theme/globalStyle and add that to the state. We’re going to default the theme (and state) to theme1 and add a copy of that into the value prop by spreading into state [ ❤️]. It should look like this:


import React from 'react'import PropTypes from 'prop-types'

import { themes } from '../theme/globalStyle'

export const SiteThemeContext = React_._createContext()




export class SiteThemeProvider extends React_._Component {state = {theme: themes['theme1']}











render() {return (<SiteThemeContext.Providervalue={{...this_.state}}>{this.props._children}</SiteThemeContext.Provider>)}}

Ok, it’s been a while since I’ve added a gif, time for another one!

And bring in the themes and add state:

Now we can add in a function to the provider to change the theme state based on what has been selected via the handleThemeChange event value:




handleThemeChange = e => {const key = e_.target.value_ const theme = themes[key]this_._setState({ theme })}

This can then be consumed by any provider that wants to use it. We’re going to need to add it into the value prop, like this:


import React from 'react'import PropTypes from 'prop-types'

import { themes } from '../theme/globalStyle'

export const SiteThemeContext = React_._createContext()




export class SiteThemeProvider extends React_._Component {state = {theme: themes['theme1']}




handleThemeChange = e => {const key = e_.target.value_ const theme = themes[key]this_._setState({ theme })}












render() {return (<SiteThemeContext.Providervalue={{...this_.state,handleThemeChange: this.handleThemeChange}}>{this.props._children}</SiteThemeContext.Provider>)}}

Ok, that is the site theme context component covered, pretty straightforward, right?

What I should mention is that the e in the handleThemeChange function is going to be the event from the theme select box that we’re about to make.

Let’s go through adding in the function and adding that to the state:

And now we can add the theme provider to src/index.js so anything lower in the dependency tree can access it via a consumer.

Add the theme select

Now we want a want to call the handleThemeChange function that is part of the SiteThemeProvider via the SiteThemeContext. I’m sure this is all making perfect sense right now (🤣) so let’s get right in there and define the component that we’re going to use to consume the SiteThemeContext.Provider with a ThemeSelect component!

In the src/components directory add a new ThemeSelect.js component. This is where we are going to consume the site theme context with a consumer.

The child of a consumer isn’t a component — it’s a function. So what we’re going to need to do is have the theme select inside the return of that function.

Let’s first set up the styled-components that will make up the select: a select box, some options, and a wrapper.

First we’ll do it without the consumer, then we’ll add it in.

**ThemeSelect.js**


import React from 'react'import styled from 'styled-components'

import { themes } from '../theme/globalStyle'




const SelectWrapper = styled_.div_`_margin: 0rem 0.5rem 0rem 0.25rem;padding: 0rem 0.5rem 0rem 0.25rem;_`









const Select = styled_.select_`margin: 1.5rem 0.5rem;padding: 0.25rem 0.5rem;font-family: ${({ theme }) => theme_.fontBody_};border: 2px solid ${({ theme }) => theme_.secondary_};box-shadow: 0px 0px 0px 1px rgba(0, 0, 0, 0.1);background: ${({ theme }) => theme_.foreground_}_;border-radius: 4px;_`



export const SelectOpt = styled_.option_`font-family: ${({ theme }) => theme_.fontBody_}_;_`















const ThemeSelect = props => {return (<SelectWrapper><Select>{Object.keys(themes)_._map((theme, index) => {return (<SelectOpt key={index} value={theme}>Theme {index + 1}</SelectOpt>)})}</Select></SelectWrapper>)}

export default ThemeSelect

So from this we can list the this themes available to us in the themes object. But that’s it, the function to handle the theme change lives on the SiteThemeProvider.

Back to the SiteThemeContext.Consumer. As I mentioned earlier, the child of a consumer is a function. ()=>() The first section is the value from the provider (<SiteThemeContext.Provider>) so let’s take a quick look at what we’ve previously defined in the provider:




value={{…this_.state,handleThemeChange: this._handleThemeChange}}

Available from <SiteThemeContext.Provider> is the state and a function. So any of those items we can extract and pass to the provider. Or to put it another way the consumer can access those values.

Here we can use destructuring to pull the handleThemeChange function we need to change the theme.

import React from 'react'

import { SiteThemeContext } from '../contexts/SiteThemeContext'







const ThemeSelect = props => {return (<SiteThemeContext.Consumer>{({ handleThemeChange }) => ()}</SiteThemeContext.Consumer>)}

export default ThemeSelect

Currently this isn’t going to change the theme because we have that hardcoded into the styled-components ThemeProvider. What we want to do is use a consumer for the currently selected theme in the SiteThemeContext.

Before that, we’ll also need to add in the onChange event we want to use to pass the event ( e) to the handleThemeChange function on SiteThemeContext.

Then in the App component we can import our <SiteThemeContext.Consumer> to consume the theme on the SiteThemeContext state and pass that to the styled-components ThemeProvider.

Want to know more?

As mentioned at the start of this article, a great resource is @leighchalliday and his YouTube channel. You can find his great use case for the React Context API.

There’s also the React community on spectrum and styled-components on spectrum.

of the walkthrough is available on CodeSandbox.

Thanks for reading 🙏

If there is anything I have missed, or if there is a better way to do something, then please let me know.

This was originally posted on my blog you can view it here please take a look at my other content if you enjoyed this.

Follow me on Twitter or Ask Me Anything on GitHub.