All right, so we looked at some ways to be accessing our pinia state. Now how do we go about mutating that state? As a refresher, when we look at how Vuex worked, when we mutated state, we would dispatch an action, that action would then go ahead and commit a mutation. And this was really the only recommended way to be mutating state unless you maybe were breaking that pattern against those recommended best practices. But of course, pinia is different. We no longer have that mutation step, and we have a lot more flexibility now with how we update our state.
The most common way to be mutating state within pinia is to trigger an action in that store, causing that state to be changed. In this example, clicking the Add to Favorites button triggers the Add to Favorites action in the Favorite Store. Now some people are surprised to learn that this is not the only way to change and mutate state within pinia. We can also change state directly by assigning a new value to the state property. In this example, we have a watcher on the city value, and then if the user deletes the city, we clear out those state properties directly so the user can start a new search.
Another way we can update state is to use the pinia patch method. This allows us to apply multiple changes at the same time to a store state. Here, notice how we're passing in an object with the updated state properties inside of it. Now, using patch comes with some added benefits because it's gonna create a single entry into the dev tools, and if we follow this convention throughout our app, we could easily do a search for patch to locate every place within our application where we're updating state in bulk like this. Another useful thing to note about patch is that it can take an object or a function as its parameter. So for example, if our state property was an array, we could use array methods to be updating our state accordingly.
Conveniently, pinia also offers a reset method. So we can reset a store's entire state to its initial value. In this example, the reset method is used within the store itself to clear out the state of our auth store. Notice how we are in an options store, and so we're using this. We can also use reset in a component like so. Using the reset method like this is super useful when we want to update an entire store at once, like when a user navigates to a certain page. For instance, let's look at this example in the router file where we use pinia's reset method in the router itself. So here we're saying whenever the user navigates back to the homepage, clear out that previous search. Now, unfortunately, the reset method is not available with setup stores, so this is one of the ways where the option store definitely has an advantage over the setup store. And if you're wondering why this is the case, it's because the reset method relies on the state function to create a fresh state, replacing the current store dot state with a new one. But we don't have access to the state function in a setup store. But if you still want that functionality where you reset an entire store, but it's a setup store, so you can't use a reset method, you could write your own setup function that achieves the same thing, where you write an action that resets the entire store, like so. When it comes to mutating our state, Pinea also offers a helpful method that we can use to get very detailed info about our actions. This is called the onAction method.
Comments