Efficient State Management With Hookstate


If you’ve worked with the React useState hook before, you might have wondered why global state management can’t be just as easy. Why do we still need so much boilerplate to manage state with the Context API? What if we don’t want to be constrained opinionated tools like Redux Toolkit or forced to use actions and reducers in our React applications? This is where Hookstate comes in. Hookstate is not just another state management solution. Apart from being feature-rich, fast, and flexible, the library takes the idea of simplifying state management in React apps to a whole new level. In this talk, I’ll introduce Hookstate as a simple and efficient state management solution for React applications.


Hey everyone, good morning, good afternoon, or good evening. My name is Evanesce Zidane. I'm a developer advocate at JetBrains for Webster Mite. And I want to talk about efficient state management with Hookstate. Okay, what's Hookstate? Well it's understandable if you've not heard about Hookstate before. Hookstate is not that old in the game. It's a state management solution for react. And one thing I really like about it, apart from Hookstate being flexible and extensible, is that it takes simplicity to a whole new level. So I want to use this to do application to demo Hookstate. I know, I know that's a draft right. It's the only way I can show you create, read, update, and delete the simplest way rather than I can show you that with a state management tool like Hookstate for example. So right in our to-do app, we can complete tasks, task updating a task, we can also delete tasks, we can add new tasks, so do stuff. And I've decided to use two different components for this application. We have the to-do component, which represents the not completed task. That's one component. We have another component for the completed task. And these two components share the same state. We're using Hookstate for state management. Now if we go back to IDE, right now I'm in a starter code base, which means I'm not using Hookstate yet. We have a starter.js file and then we have tasks inside of it. What if global state management was this easy? What if this is all we need to do to manage global state? We just have a variable and it's named tasks or whatever state we want. And we export it, then we go back to different components and all we got to do is import it and use it. Same thing with the not completed component. This is not Hookstate yet. This is just javascript. Using javascript variables, export and import. You've probably used Redux before or react, Context api or RxJS and all the rest. The state management, it works. It's really efficient state management with these tools. But it's complex. It's really complex. And I want it to be this easy. I wanted that if I want to add a new task, I have just one method like we have on line 10. I just do task.push and then I have my new task instead of using extra slices, reducers, actions and all the rest or the spread operator having a previous state and then the next state and all that. Can I just do task.push and have my new task? This is why I love Hookstate. Now let me go back to the main branch where I have that should be this. Where I have the implementation for Hookstate, this application built with Hookstate and right in the store.js file. This is how we're using Hookstate to create a global state. We have store and that's equal to the create state method from addHookstate slash core. So you want to install that npm installs slash core or yarn addHookstate slash core. And why we have slash core right now is because Hookstate, remember I said it's very extensible. So we also have Hookstate slash persistence, which helps us to persist states in the local storage, the browser's local storage. How cool is that? But what we need right now is Hookstate slash core and the create state method from Hookstate slash core. This is all we got to do to create a global state. We have tasks right inside of the object for create state method. So why I use an object is because I'm going to add other things to let's say this is a global store. And I want to add other things like something else. And this is my new state. Right now this is my new state I can whatever whatever methods Hookstate makes available to store which which contains all the other states is also made available to tasks and to something else. So if we come over to the apple.js file, we were adding a new task and on line 12. It's that simple. So add a new task, we have task dot merge, and a new task. That's all. Right now on line seven, we are using the use state hook from Hookstate to get the value of tax. So we're going to use tax from a global store we have const and then we're destructuring directly we can say we can decide to say this is const state is equal to use state which and then the store we're importing from the slash store and then we decide to do store dot tasks. Yeah, but what we decided to do is destructure directly and then right here we have tax and we can use that. Now in Hookstate to get the value of a state, we need the get method. So right here we have input value dot get and that gets the value of this local state and create an online eight, we can create global states, we can create local state, I'm creating the state for the input value and I just do that directly with the use state hook. I don't need the create state for this one. I wanted to be a global state, I use the create state method. I use the use state hook and then I have my local state and input value dot get gets the value of my local state. And you can see right here on line 27, where I'm setting the value of input value to be whatever I use that types inside of the input field and I'm using input value dot set. That's all that's all I got to do. And we have the completed component and the not completed component, both of them use store. And all we got to do is import store from dot dot slash store, which is the storage yes file. And on line five, we have tasks from store. And then for completed task, what we're doing in this case is we're doing using tasks dot gets to get to get all the tasks and raising the filter method to get just the completed task. There's probably a more straightforward way to do this, but I want to show you how to work. I'm working with two different components and using that to access one store and for remove task. What we're doing with hook states to remove the task, raising the nested property from tasks to get the task we want to remove. So that is in task index. And how are we getting the task index on line nine, we have tags dot get dot index of this is a javascript method. So dot index of task and as a task, we're going to be getting from the remove task function argument when we call it right here on line 21. So remove task and task. So this is a particular task when we move, we get its index, we use the nested property from tasks to set the value of that particular task to none. This is how we delete something in hook state, we use the non value from hook state slash core. So we call the set method and then its argument is none. If you want to set this to something else, say, hey, this is going to be the new value of that particular task, but want to remove the task, and we have none. And that's it. This is me using hook state to access a store, we get in all the tasks. And so we use the get method to get the value of the task. Now we're filtering to get the completed task to update a task, we have to dot set methods, and then that's going to be the new value. Now to remove a task, we have the non value from hook state slash core. And that's all we're going to do to use hook state for state management. I have a longer tutorial on hook state on my YouTube channel. So yeah, you can check it out if you want to see if you want to see more about hook state, you want something more extensive. I also have an article on the subject on hook state. So do me a favor and check out my YouTube channel. I have other react state management tutorials. And also check me out on LinkedIn, GitHub, and Twitter. You'll find the various articles I've written on my GitHub profile. So you go to github.com slash evidence is done. I have a link there to all my articles. And this has been nice. That's me demoing hook state for efficient state management in react apps. Please check out hook state. It's really nice work that the team behind it are doing it fast is efficient. I've used it and it works. It's been working well for me. Alright, so that's it. See you next time. Bye.
10 min
25 Oct, 2021

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Workshops on related topic