So, this is a really short presentation on VALTEO, which is technically a global state but the really wonderful part about it is you can actually treat it as a micro-state management, which sort of means that you only use what you need and you don't have to change your whole paradigm.
So, a little about me. My name is Nia, I live in Toronto, I used to live in Atlanta in the US and now I work for a company that's called Healthy and they do telehealth and they're based out of New York. So, travel all over the place. And I work as a front-end tech lead and architect.
So, Valtio is sort of how we've introduced state management to our legacy application as well. So, what is Valtio, right? Right here I've got the GitHub link to Valtio as well, so if you're ever interested, that's really cool. But the best part about Valtio is it's a proxy-based state management system. It uses JavaScript's native idea of proxies to create observable state. It's immutable state. So, it lets you update the state basically how you'd update an object, but when the state changes, everything is logged and new state objects are created rather than modifying existing ones, right? And the best part about it is overall minimal boilerplate.
So, even with Redux or all the other ones, like, they are great in their own ways, but there's so much setup, and especially if you're working in really old React applications where there's still a lot of class components, it's super easy. There's not a lot of stuff to, like, configure and it just starts working. But let's dig in to the concept of JavaScript proxies. It was actually introduced in ES6, and at least the way I've thought about it, and it sort of helped me understand is it's sort of a shield around your object. It sort of protects your object. Basically, it changes what you see when you look at the object, properties or values, for example, and you can make the object look different, add extra information or even hide certain parts. The other part of it that's really cool is now you can also pass in handler functions and controls what happens when you try to do things with the object, like changing a property, reading a property, or calling a function, right? So you can add rules and checks and balances to it, so it protects your object as you try to change it.
So here, that same code, the thing I just wanted to show you here is I've set up my object target, I've got a handler function, and I've set it up inside my proxy, and the thing to note here is message two here says everyone. Usually, when you do target.message two, you'd get everyone as a response, but because it goes through my proxy, when I do proxy.message two, I'm actually gonna get world rather than everyone, because of how the proxy states right here, the handler function, it states if the prop equals message return world. So even though target.message two is everyone, the proxy protects it and says proxy.message two, which is taking target here, will return world. So this is sort of the foundation of how Valteo works, but this whole boilerplate here, they've actually made it much simpler for us. Let's look at how it looks. Well, before that, why this matters is this works with class components. I know with a lot of Redux stuff or React query even, everyone is on hooks, but if you're still in legacy large applications or you're transferring over and you're trying to manage tech debt, this is a great way of trying to find balance between the two while you're in transition, but you still get aspects of global state management in a class component or in vanilla JavaScript components and still able to move forward. So let's just look at how the React implementation is. One, here you can see, it's just import proxy, and Valtio is actually set up all that boilerplate for your proxy part, so you don't have to do any of that work. I've got my person state proxy here, and my auth state, which has a nested proxy, which is user person state, right? So, the way I would mutate it or access it, as it's set here, is auth state.userDate.name equals Nina, right? That's a direct object mutation, and that's it. That is literally how you change your state management, your state values in your state.
Comments