So for today, let's go back to state. And before we actually go into details, let's also look at what state actually is because state can be a lot of things, and state often can also be living just inside a menu. So I think two years ago, I was creating a nice open source library for the city of Amsterdam, so also the city where I'm living in, and over there, we were working with state, of course.
We also created a component library so in that we also worked with menus and menu items inside such a menu. And the menu looks something like this. So you would have a simple menu that could be open or it could be closed, and whether it was open, the state went through, and we decided to display a submenu. And in this solution, we almost always pass state down. So you can just create a menu, and you pass state down, whether or not the menu's open, and then based on that, you will do something inside maybe a child component, you maybe render a child component.
And in this solution, you almost always use local state, so either a state in a class-based React component, or maybe if you're using function components, then you would use the useState hook. And I think for simple things like this, like having a menu, having a submenu, or maybe just a menu item, you always choose to use local state because it doesn't really make sense to implement something bigger, like Redux or the Context API or maybe one of those other tools that are around. But if you have a more complex menu, you already need to consider if you still want to be using simple local state in React. Because maybe you have a menu, you'd have a submenu, there's a menu item, or maybe the menu item inside the submenu is another submenu, so in theory, we could go indefinitely far.
And in this solution, it's already good to start thinking about state like how complex should my state be? Or how simple should it be? Because this could be an example of your menu. So maybe you have a hamburger menu for mobile devices. It could be open or it could be closed. So you still have simple state like open is true or false. But maybe you also have a child inside that could be open or closed, so maybe you also have state for the opened or closed child in there. So this is already starting to look a bit more complex, and especially if you want people to stay in the same state whenever they refresh the page. I think it's getting more and more complex, and those are things you should think about.
And also you might be using, if you worked in a component library before, you probably have been wondering about accessible components. So if you want your component to be accessible, you want certain parts to be highlighted whenever someone uses the keyboard to navigate instead of their mouse. If you would be in such a situation that maybe you also want to know which child is selected, so you don't only want to know if the menu is opened, if a submenu is opened, or actually which submenu is opened, but you also want to know which child is selected. And it goes pretty far, and it also depends on how far you want to go, because I don't think you need to define IDs in state to determine which submenu is opened, but maybe if you do want to do this, it already gets more complex.
And this is still something you can quite easily use local state for. But there's already something you need to think about if you would still pass the prop zone. And I think for these scenarios where you would have a menu with a menu item, or maybe a menu with a submenu, or multiple submenus and menu items inside there, I think it's probably straightforward to say you could just pass state down, you create local state in the parent components, and in there you pass it down as props. And in the parent component, you will manage the local state, you will make sure that the menu items are opened or closed whether or not the actions someone is taking in your application. But what if these components aren't menus or submenus or menu items? So what if these components are your entire application or maybe even routes inside your application or components inside routes inside your entire application? If you would be in such a situation, then probably you won't be using local state anymore because you want your state to be more diverse.
And when we talk about more diverse, I always think about the chessboard. So chess has been becoming more and more popular, I think, especially with all the Netflix shows and all the cool documentaries about it. So when you think about state, you can also think about a chessboard. So do you want your state to be simple? Do you want your state to be able to pass one field by one field or one component by one component? It's actually the most basic piece in chess is, or do you want your state to be more complex? Do you want it to be able to maybe skip components or skip fields to be able to go back and forth? Is that something you want your state to do? And if that's a scenario, then you won't be helped enough by using just local state with maybe you state hook or maybe even you seducer hook. That's a scenario you probably want to do more and you actually need more.
And with that scenario, maybe you could have ended up using context API or using use reducer, but before those functions were available, you probably ended up with using something like Redux or MobX, or if you're using GraphQL, something like Apollo. And all these libraries were created because React lacked certain futures and able to do state management. Especially in able to do state management effectively. So I think the context API helped us a lot by adding these additional features and I will show you how you can use it.
So the agenda for today, we will be mostly giving you a more introduction for about 30 or 40 minutes, and afterwards, I'll be working on exercises in breakout rooms. And then during in between, we can sync to look at the exercises. So depending on how fast everyone will go today, maybe we won't be able to do all the exercises at once. Luckily, we have the Discord channel, and I think I shared it in the chat already, and Lira was also there. So if you're not in the Discord channel, then please let her know through Discord, and then she can add you to the channel, and we can discuss the exercises even after today's workshop. So today's workshop will be mostly about the context API and how you can use it to do state management effectively in any React application. And actually, before a couple versions of React ago, it was advised not to use context because if you want the application to be stable, don't use context, it's experimental.
And I think it was a really good message because a lot has been changed to the context API. And some people even still say you shouldn't use it for state management, but I think it's also a bit of a more opinionated question to ask to a lot of people. But before this React version, it was actually advised not to use React, a context API for state management. And actually React was also telling you, or at least the React documentation was telling you, if you aren't familiar with state management libraries like Redux or Mopix, don't use context and just use these libraries because they are better tested, they're built by the community and they might have a lot of features that you otherwise wouldn't have. So how does the context API work? So the context API can be seen as sort of state or local state, or doesn't necessarily have to be local state, but could be local state, that wraps multiple components. So from multiple components, you should be able to access this context value and also to access functions to update the context value.
Comments