With React 16.8, we got something called Hooks. Hooks allow developers to mimic class component functionality inside a functional component.
One of those hooks is the useContext hook which allows you to connect a functional component to a context.
const value = React.useContext(MyContext);
From the documentation:
Answer from user6612182 on Stack OverflowAccepts a context object (the value returned from
React.createContext) and returns the current context value for that context. The current context value is determined by the value prop of the nearest<MyContext.Provider>above the calling component in the tree.When the nearest
<MyContext.Provider>above the component updates, this Hook will trigger a rerender with the latest context value passed to that MyContext provider.
reactjs - How to access values from context in a separate functional component - Stack Overflow
reactjs - Context in "stateless" component? - Stack Overflow
reactjs - How to change context value in functional component? - Stack Overflow
Can React Context be accessed outside of a react component?
Videos
With React 16.8, we got something called Hooks. Hooks allow developers to mimic class component functionality inside a functional component.
One of those hooks is the useContext hook which allows you to connect a functional component to a context.
const value = React.useContext(MyContext);
From the documentation:
Accepts a context object (the value returned from
React.createContext) and returns the current context value for that context. The current context value is determined by the value prop of the nearest<MyContext.Provider>above the calling component in the tree.When the nearest
<MyContext.Provider>above the component updates, this Hook will trigger a rerender with the latest context value passed to that MyContext provider.
// first define your context
const MyContext = React.createContext();
// wrap your component that should use the context
<MyContext.Provider value={yourValue}>
<YourComponent />
</MyContext.Provider>
// then inside YourComponent call useContext hook
import {useContext} from React
function YourComponent() {
const contextValue = useContext(MyContext)
return <div>{/* anything */}</div>
}
Please refer to this link: https://reactjs.org/docs/context.html
Read the documentation: createContext, useContext. You need to render a ContextProvider in your parent (or top-level) component, then you can get the data in any component in the tree like const { theme } = useContext(ColorModeContext);.
You don't need to pass the mode as props, put it as one of the values in the context and access it.
Here's how you would render it in your example:
<ColorModeContext.Provider value={{colorMode, theme}}>
<Header />
</ColorModeContext.Provider>
You can pass an object inside the value in the context provider, in other word you can pass the toggle function inside your value to be consumed in the childern. thus you gain an access to change your mode state. Note that the way changes are determined can cause some issues when passing objects as value, this might trigger unnecessary rerendering see Caveats for more info. or refer to the useContext docs
<ColorModeContext.Provider
value={{ colorMode: colorMode, toggleColorMode: toggleColorMode }}
>
<ThemeProvider theme={theme}>
<Header />
</ThemeProvider>
</ColorModeContext.Provider>
What you have provided under your definition of "Stateless:" function is not what a Stateless function is. You have provided your action creator as a thunk. I assume you wanted to insert the code for your Client component instead. To access context in a stateless component, your Client component would do something like this (which is documented here)
const Client = (props, context) => {
return <div >{context.localizedString("someKey", "someFallback")} </div>
}
Client.contextTypes = {
localizedString: React.PropTypes.func
}
export default Client
I had the same question. The modern way (in 2019) is to use hook useContext(contextName). Docs: https://reactjs.org/docs/hooks-reference.html#usecontext
const dumbComp = (props) => {
const context = useContext(contextName);
return(
<div>
...
</div>
);
}