Home

Context API

tl;dr

Create context and use the Provider and Consumer.

import React from 'react'; const ThemeContext = React.createContext( /* optional default value */ ); const App = props => ( <ThemeContext.Provider value={{ primaryColor: green }}> {props.children} </ThemeContext.Provider> ); const ThemedButton = () => ( <ThemeContext.Consumer> {value => ( <Button primaryColor={{ value.primaryColor }}> I'm button using context! </Button> )} </ThemeContext.Consumer> );

Repository

https://github.com/okeeffed/developer-notes-nextjs/content/react/context-api

Sections


Related