Mid From PDF React React.js

Can you explain the useContext hook?

useContext lets you consume a context value without using a Context.Consumer.

✅ Example:

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

function App() {

return (

<ThemeContext.Provider value="dark">

<Toolbar />

</ThemeContext.Provider>

);

}

function Toolbar() {

const theme = useContext(ThemeContext);
return <div className={`theme-${theme}`}>Theme is {theme}</div>;
}

More from React.js Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details