Skip to main content

Action

The objects ultimately dispatched to Zedux stores.

Actions can also be wrapped in ActionChains, which can contain additional metadata. Zedux uses this pattern internally for its store composition model.

tip

Actions are ActionChains with a size of 1.

Definition

export interface Action<
Payload = any,
Type extends string = string,
Meta = any
> {
meta?: Meta
payload?: Payload
type: Type
}

Zedux actions must contain a string type property and may contain optional meta and payload properties.

Examples

const normalAction: Action = {
type: 'todos/add',
payload: { text: 'be awesome' },
}

const withMeta: Action = {
type: 'todos/add',
meta: {
dispatchedFrom: 'MyComponent',
},
}

const typeOnly: Action = {
type: 'dropdown/toggle',
}

Creation

You don't need to create action objects manually. Zedux provides a high-level actionFactory() "factory factory" for action creation:

import { actionFactory } from '@zedux/react'

const test = actionFactory('test')
test('payload') // { type: 'test', payload: 'payload' }

const addTodo = actionFactory<Todo>('todos/add')

store.dispatch(addTodo({ text: 'be awesome' }))

See Also