React

useContext

useContext is a hook that lets you get data from a React Context

// Calling signature
const contextValue = useContext(context)

Basic Usage

(() => {
const themes = {
light: {
foreground: "#000000",
background: "#eeeeee"
},
dark: {
foreground: "#ffffff",
background: "#222222"
}
};
const ThemeContext = React.createContext(themes.light);
function ThemedButton() {
const theme = useContext(ThemeContext);
return (
<button style={{ background: theme.background, color: theme.foreground }}>
I am styled by theme context!
</button>
);
}
function Toolbar(props) {
return (
<div>
<ThemedButton />
</div>
);
}
function App() {
return (
<ThemeContext.Provider value={themes.dark}>
<Toolbar />
</ThemeContext.Provider>
);
}
return App
})()
Previous
useRef