context-api-polyfill
This library polyfills the React < 16.0.0
with the new context api. Thus, it is possible to use this feature and still have releases to react 15 as non-breaking change.
Caveat
The context api is based on a Provider and a Consumer which obey the closure defined by the browser DOM Tree. In this matter, it can be seen as a closure isolating each provider that is being wrapper by another provider.
To mimic the closure created by the DOM Tree during render, it is being used the stack data structure. Everytime when a provider in the virtual DOM Tree runs its lifecycle methods componentWillMount
or componentWillReceiveProps
, it is being added to the stack. Also removed when componentDidMount
or componentDidUpdate
.
I highly recommend setting the initial value to the context created and use that to set value to the component that is resposible to feed the provider with value.
;; // Here is your initial valueconst INITIAL_VALUE = "red";// As expected by the createContext function, you pass down the value that is going to be set as default.const BackgroundContext = React;// Also, set this value as initialValue to your context objectBackgroundContextinitialValue = INITIAL_VALUE; ;
Doing it so, you can use as:
import React Component from "react";import PropTypes from "prop-types";import BackgroundContext from "./BackgroundContext"; static propTypes = children: PropTypesnodeisRequired ; state = // Setting initial value to the state backgroundColor: BackgroundContextinitialValue ; { this; }; { const children = thisprops; const backgroundColor = thisstate; return <div> <button ="button" => Toggle Bg Color </button> /* feeding back the value according to the toggle color value */ <BackgroundContext.Provider => children </BackgroundContext.Provider> </div> ; }
For more information, check out the DEV
folder in this project GitHub Repo.
API
Test
npm test