DEV Community

HandsomeTan
HandsomeTan

Posted on

Three ways to mutate pinia's state value

  • the first method, directly assigning value(not recommended): useStore().stateItem = newVal.

  • the second, using $patch method. The parameter types of $path method has two ways:useStore().$patch(stateObject), which requires passsing an entire state object; The second type: useStore().$patch((state) =>{state.item = newVal}).

  • the third, calls to pinia's actions:

const useStore = defineStore('users', {
  state: ()=> ({
    count: 0
  }),

  actions: {
    increment: (newVal) => {
       this.count = newVal
    }
  }
})
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay