Skip to content

Latest commit

 

History

History
62 lines (53 loc) · 1.09 KB

consuming-multiple-contexts.md

File metadata and controls

62 lines (53 loc) · 1.09 KB
const ThemeContext = createContext('light')
const UserContext = createContext({ name: 'Guest' })
const Content = () => {
  const theme = useContext(ThemeContext)
  const user = useContext(UserContext)
  
  return (
    <ProfilePage
      user={user}
      theme={theme}
    />
  )
}

Or:

const Content = () => (
 <ThemeContext.Consumer>
   {theme => (
     <UserContext.Consumer>
       {user => (
         <ProfilePage user={user} theme={theme} />
       )}
     </UserContext.Consumer>
   )}
 </ThemeContext.Consumer>
)
const Layout = () => (
  <div>
    <Sidebar />
    <Content />
  </div>
)
const AppPage = ({ signedInUser, theme }) => (
  <ThemeContext.Provider value={theme}>
    <UserContext.Provider value={signedInUser}>
      <Layout />
    </UserContext.Provider>
  </ThemeContext.Provider>
)




Prev - Updating Context from a Nested Component | Next - useReducer