Nesting and overriding new React Context API

iamandrewluca profile image Andrew Luca Updated on ・2 min read

While learning react-router v4 I read some of their source code. And as we know they are using current context for passing down router and route info overriding previous/parent route info

GitHub logo ReactTraining / react-router

Declarative routing for React


React Router

Declarative routing for React


View the docs here

Migrating from 2.x/3.x?

3.x docs

2.x docs


This repository is a monorepo that we manage using Lerna. That means that we actually publish several packages to npm from the same codebase, including:

Package Version Docs Description
react-router npm The core of React Router
react-router-dom npm DOM bindings for React Router
react-router-native npm React Native bindings for React Router
react-router-config npm Static route config helpers

Redux users: The react-router-redux package is now deprecated. See Redux Integration for a better approach.


Detailed release notes for a given version can be found on our releases page.

For change notes prior to version 4, please see the CHANGES.md file.


You may provide financial support for this project by donating via Open Collective, attending one of our workshops, or purchasing one of our online courses. Thank you for your…

getChildContext() {
  return {
    router: {
      route: {
        location: this.props.location || this.context.router.route.location,
        match: this.state.match

React team announced new Context API that no longer will be deprecated in React v16.3.0, that is already released :)


Now I was thinking how ReactTraining will make this overriding using new Context API.
From start I used create-react-context polyfill for new context. It works exactly, just change the import.

import { render } from "react-dom";
import React, { createContext } from "react";
// import createContext from "create-react-context";

Next we need to create the context. Context has a Consumer and a Provider

const { Provider, Consumer } = createContext();

Provider is used to pass to him some data in value prop

render() {
    return (
        <Provider value={"React is Awesome!"}>
            nested content...

And Consumer is used to consume that value using render props

render() {
    return (
            {(theValue) => {
                return theValue

        // shorthand
            {theValue => theValue}

// output
// React is Awesome!

We may use the Consumer how many times we want.

Now back to our overriding. Here is my app

const App = () => (
  <Provider value={{ location: "/" }}>
      <NestedPath location="haha/">
              <NestedPath />

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

And here is the output


And this is my NestedPath component

const NestedPath = ({ location = "location/", children }) => (
        {router => (
                <Provider value={{ ...router, location: router.location + location }}>
                    {children || null}

Here as you see inside Provider we override previous one with a new value. And all child Consumers now will take the new value.

Here is a sandbox to play with


Thanks for reading!!! This is a duplicate of my Medium Story!
dev.to is new medium for developers :)

Posted on by:

iamandrewluca profile

Andrew Luca


All in Developer! ♣️♥️♠️♦️


markdown guide

Hey man, great tutorial! I was searching for something like this. Thank you!



How we can add API to the React Redux and give some example?


What do you mean? Is this related to React Context API?


Can you Please explain with Register Form?


Please explain your use case and I'll try to help. Give me some context of what you want to do.

Example In Registration Form When i am trying to register with the Name,Email and Phone Number it can't stored in redux could you please explain How to perform GET and POST methods into my redux using API calls.

Here is my code..

export function setName(name)
payload: new Promise((resolve,reject)=>{