Skip to content

@pinia/root / pinia / mapActions

mapActions()

Allows directly using actions from your store without using the composition API (setup()) by generating an object to be spread in the methods field of a component.

Param

store to map from

Param

array or object

mapActions(useStore, keyMapper)

ts
function mapActions<Id, S, G, A, KeyMapper>(useStore, keyMapper): _MapActionsObjectReturn<A, KeyMapper>

Allows directly using actions from your store without using the composition API (setup()) by generating an object to be spread in the methods field of a component. The values of the object are the actions while the keys are the names of the resulting methods.

Type Parameters

Id extends string

S extends StateTree

G extends _GettersTree<S>

A

KeyMapper extends Record<string, keyof A>

Parameters

useStore: StoreDefinition<Id, S, G, A>

store to map from

keyMapper: KeyMapper

object to define new names for the actions

Returns

_MapActionsObjectReturn<A, KeyMapper>

Param

store to map from

Param

array or object

Example

js
export default {
  methods: {
    // other methods properties
    // useCounterStore has two actions named `increment` and `setCount`
    ...mapActions(useCounterStore, { more: 'increment', setIt: 'setCount' })
  },

  created() {
    this.more()
    this.setIt(2)
  }
}

mapActions(useStore, keys)

ts
function mapActions<Id, S, G, A>(useStore, keys): _MapActionsReturn<A>

Allows directly using actions from your store without using the composition API (setup()) by generating an object to be spread in the methods field of a component.

Type Parameters

Id extends string

S extends StateTree

G extends _GettersTree<S>

A

Parameters

useStore: StoreDefinition<Id, S, G, A>

store to map from

keys: keyof A[]

array of action names to map

Returns

_MapActionsReturn<A>

Param

store to map from

Param

array or object

Example

js
export default {
  methods: {
    // other methods properties
    ...mapActions(useCounterStore, ['increment', 'setCount'])
  },

  created() {
    this.increment()
    this.setCount(2) // pass arguments as usual
  }
}

Released under the MIT License.