Skip to content

Instantly share code, notes, and snippets.

@DanC5
Created November 1, 2019 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanC5/ba1e3d24ca7d8349701dacb2d529df81 to your computer and use it in GitHub Desktop.
Save DanC5/ba1e3d24ca7d8349701dacb2d529df81 to your computer and use it in GitHub Desktop.
branch-context-stream-context-blog
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