First Impressions: React Context API
Tags: react, context, javascript, webdev
Published: 2018-05-14 13:01:24

---
title: 'First Impressions: React Context API'
subtitle: You probably don't need Redux...
teaser: This post I check out the React Context API. No it does not replace Redux but you probably won't need to reach to Redux as fast anymore.
canonical_url: https://www.johnstewart.io/first-impressions-react-context-api
published: true
cover_image: https://thepracticaldev.s3.amazonaws.com/i/dl3n4i4eqrolc23jhl3d.png
featuredImageCredit: Photo by Alex Knight on Unsplash
type: post
tags: 
- reactjs
- context
- javascript
- webdev
---
*Photo by Alex Knight on Unsplash*


React came out with the new Context API about [a month ago](https://github.com/facebook/react/releases/tag/v16.3.0). The past few days I've been playing around with it and have developed some understanding of how it works. It introduces some new use cases to consider and helps with some old ones as well. This post I get into my thoughts on it and how ultimately I think our developer lives are better with the new Context API.

## New API

The new [Context API](https://reactjs.org/docs/context.html#api) is made up of these three parts:

- [React.createContext](https://reactjs.org/docs/context.html#reactcreatecontext)
- [Provider](https://reactjs.org/docs/context.html#provider)
- [Consumer](https://reactjs.org/docs/context.html#consumer)

Using the above three pieces we can really do a lot now. Let's take a quick look at how they can be used.

```js
import React from 'react';
import { render } from 'react-dom';

const theme = {
  buttonColor: 'blue'
};

const ThemeContext = React.createContext(theme);

const AppButton = () => (
  <ThemeContext.Consumer>
    {({ buttonColor }) => (
      <button style={{ background: buttonColor }}>App Button</button>
    )}
  </ThemeContext.Consumer>
);

const Container = () => (
  <div>
    <AppButton />
  </div>
);

const App = () => (
  <div>
    <h1>My App</h1>
    <ThemeContext.Provider value={theme}>
      <Container />
    </ThemeContext.Provider>
  </div>
);

render(<App />, document.getElementById('root'));
```
[![Edit 387l8656xp](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/387l8656xp)

Above is a basic example of creating some context and then using it. Line 8 I create some context for the app theme. Creating the context gives me two new React components `{ Provider, Consumer }`. On line 27 I use the `Provider` to allow for child components to have access to the theme context we created. Then on line 11 within my `AppButton` component I use the `Consumer` part of the context to gain access to the theme information. This example is small but illustrates the basic setup behind using the new Context API.

## Familiarity

If you've ever setup a [React Redux](https://github.com/reduxjs/react-redux/blob/master/docs/api.md#provider-store) project then that `Provider` part of the API should look pretty familiar. I'm not sure of how the internals work specifically in either code base but it's clear that the intent is the same in React and [Redux](https://redux.js.org/).

Where they differ is how both want children components to consume state. Redux is more opinionated on how state is updated through reducers and actions but React allows you to read and update state how you see fit.

Taking this a step further Redux exposes a higher order component to connect to the store but in React they expose the state through a child function. Both achieve the same goal but again Redux is a bit more prescriptive.

The broad strokes of this API should be somewhat familiar to you if you have had to deal with or setup a state management library before.

## Roll Your Own State Management

With the Context API you can manage small application state much easier now. If you have a small single page application and it's just you or a small team working on it then I think using the Context API should be enough to manage all your app state.

That being said, you should come up with a pattern that you understand or pull concepts from things such as Redux or other state libs and try them out at your scale.

Ultimately it's about what makes you comfortable and works for the app. If Redux is your bread and butter then go for it. If you are just starting with React and find yourself wondering how to pass state over here and down there then check out the Context API for now.

If at some point you feel it's not enough, google "React state management libraries" and try a few.

## I like Redux

I've been using Redux for awhile now and I like how it works. I like Redux so much that I've used it for projects that don't use React at all. Redux is a great state management library and does exactly what I need it to do. The only thing I didn't like with Redux was the excess files I would create for actions and reducers. I solved this by using [Ducks](https://github.com/erikras/ducks-modular-redux). It's a nice pattern for Redux and my preferred approach. Check it out!

The Context API will make me reach for Redux a lot less now. In a team environment or on a project of medium to large size I'll still use Redux. At the end of the day you need to do what makes you and your team comfortable and ultimately makes your lives easier.

## Final Thoughts

All in all I like the new Context API and think it provides a ton of value. For those just getting started with React it should be able to answer the global app state question much faster then passing props way down and learning a state management library.

Check out some links below to learn more about the Context API:

- [React Context Docs](https://reactjs.org/docs/context.html)
- [New Context API by Kent C. Dodds](https://medium.com/dailyjs/reacts-%EF%B8%8F-new-context-api-70c9fe01596b)

If you liked this article then please show some ❤️. If you didn’t then lets talk about it 😃.

Originally posted on my blog at [johnstewart.io](https://goo.gl/U7Lyby).