31
loading...
This website collects cookies to deliver better user experience
Context provides a way to share values between components without having to explicitly pass a prop through every level of the tree.
props
every intermediate component becomes aware of it, even if it’s not being used at that level. That adds coupling between the different components that form that part of the page.Provider
where we keep the relevant state. It is equivalent to the store in Redux. It can be statically initialized or fetched dynamically. Combined with the useState
and useEffect
hooks, we can build the logic we need to keep it up-to-date with little effort.Consumer
. Any component that is rendered below the Provider
has access to the information stored in it thanks to it. Another hook, useContext makes the syntax even more lightweight. Consumers get re-rendered on updates to the state.Provider
, every component that needs it can access this state easily.Provider
that wraps the page and fetches the data asynchronously by contacting a backend for frontend. That information is stored in the provider. After this, every component on the page uses the data without having to worry about the remote interaction. Thus, we contain that to a component that we test dedicatedly, making the others more independent.Provider
changes, the whole tree under it will be re-rendered. If you save state that frequently mutates high up in your component hierarchy, that’ll lead to flickering. It may compromise the experience of the application. Measuring the performance of your application regularly helps to prevent nasty surprises.Provider
with a function to change the state. That way, the components are not merely passive consumers. And closing the cycle, we might long at some point for Redux’s Action -> Reducer -> State loop. We can do this as well without Redux.