Рет қаралды 199
Want to master UseContext in just 90 seconds? Dive into this video to discover how the UseContext hook works with React's Context API to share values across components. Learn how to efficiently manage themes like dark and light throughout your entire application using context providers.
Subscribe now to level up your React skills in no time! / @boogcodes
Transcript:
You want to learn useContext in 90 seconds? HERE. WE. GO.
The useContext hook allows you to work with react's context API. This is a mechanism that allows you to share or scope values throughout the entire component tree.
Lets imagine we have an object called "theme" that can be dark or light. To share the current theme across multiple disconnecting components, we can create a context. One part of the application might be dark, so we use a context provider to scope the dark theme there.
Now, any child component inside of it can inherit that value without needing to pass down props to the children.
This brings us to the useContext hook. It allows us to access or consume the current value from the context provider. Which may reside many levels higher in the component tree. Reading a parent value from useContext is a whole lot easier than passing props down through multiple children.
Now, the theme changes from dark to light in the parent provider. The value here will be updated automatically. Now, if you've ever used react context in the past, you've likely used the consumer component. The useContext hook is pretty much just an easier replacement for the consumer.
useState() is the most important hook to learn for React. useState is the most commonly used hook as well, and you can learn it in 90 seconds by watching my useState video next!