In the Pinia documentation, you can read that you can modify the state directly via ...mapWritableState, or with store.$patch(), I've always learned that you shouldn't modify the state directly, but use 'actions'.
Can you help me understand the advantage of modifying the state directly with mapWritableState instead of using an action?
Thank you in advance.
Being able to directly modify the state (or using patch) is intentional to lower the entry barrier and scale down. After many years using Vuex, most mutations were completely unnecessary as they were merely doing a single operation via an assignment (=) or collection methods like push. They were always perceived as verbose, no matter the size of the project, adding to the final bundle size as well, and were useful only when grouping multiple modifications and some logic, usually in larger projects. In Pinia, you are still able to have these groups by using actions but you get to choose if you need them or not, being able to start simple, and scale up when necessary.
So I want to make people realise mutations is an unnecessary concept thanks to actions and that the cost of mutations and its verbosity is not enough to justify the possibility of organisation it brings. On top of that, one of the main aspects of mutations was devtools: being able to go back and forward. In Pinia, all modifications are still tracked so doing direct state modification still allows you to keep things organised despite it wasn't taught that way with Vuex
Eduardo San Martin Morote, member of VueJS core team said this.
See: https://github.com/vuejs/pinia/issues/58#issuecomment-577074138