From the course: React Best Practices by Pearson

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Manage state with Context

Manage state with Context

All right, so now that we've seen how to use the use state hook for managing very simple state in a react application and some of the details and best practices around that. The next thing we're going to do is take a look at how you can solve this problem of props drilling using context. All right, so with props drilling again, the problem is that the component that's controlling the state and the component that needs the state are relatively far away from each other in the component tree, right? So, you know, uh, let's imagine here that we want to add a dark mode button or option to our app. Well, as you can probably imagine that dark mode button, right? That toggle switch is probably going to be inside a nav bar, which is going to be inside, you know, a router, which is going to be inside the app itself. So already that button is pretty deeply nested. And then imagine how deeply nested some other parts of the app might be, right? You could have text inside some sort of list item…

Contents