0

How can I use other getter inside setup store in Pinia?

export const useCounterStore = defineStore('counter', () => {
  const count = ref(0)
  const name = ref('Eduardo')
  const doubleCount = computed(() => count.value * 2)
  const HOW_TO_USE = computed(() => { /* how to use doubleCount here? */ } )
  function increment() {
    count.value++
  }

  return { count, name, doubleCount, increment }
})

Edit - follow-up

If I want to pass an argument to that HOW_TO_USE , then should it be just returning a function like this?

const HOW_TO_USE = computed((arg)=> /* do with arg */) 

or actually this:

const HOW_TO_USE = (arg) => computed((/*no args*/)=> /* do with arg */) 
wildfluss
  • 107
  • 8
  • 1
    The same way you'd do without Pinia as doubleCount.value, setup function basically wraps composition api – Estus Flask Feb 26 '23 at 20:25
  • Makes sense! Thanks! And if I want to pass an argument to that it then would be just returning a function like this? const HOW_TO_USE = computed((arg)=> /* do with arg */) ? – wildfluss Feb 26 '23 at 20:54
  • Then you need a parametrized computed, something like https://stackoverflow.com/a/74728648/3731501 – Estus Flask Feb 26 '23 at 22:58

0 Answers0