useContext

Accepts a context object created by React.createContext and returns the current context value.

Syntax

react
const value = useContext(MyContext)

Example

react
const ThemeContext = React.createContext("light");

function ThemedButton() {
  const theme = useContext(ThemeContext);
  return <button className={theme}>Click</button>;
}