Created
November 1, 2019 19:14
-
-
Save DanC5/ba1e3d24ca7d8349701dacb2d529df81 to your computer and use it in GitHub Desktop.
branch-context-stream-context-blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { createContext, useState } from "react"; | |
// create a React context for managing global state | |
const BranchContext = createContext(); | |
// access context value through a consumer component | |
export const BranchConsumer = BranchContext.Consumer; | |
// save data and share with children through provider component | |
const BranchProvider = ({ children }) => { | |
const [contextData, setContextData] = useState({}); | |
const contextValue = { contextData, setContextData }; | |
return ( | |
<BranchContext.Provider value={contextValue}> | |
{children} | |
</BranchContext.Provider> | |
); | |
}; | |
export default BranchProvider; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment