Skip to main content

ActionFactory

The function returned from the actionFactory() factory.

An action factory is just a function that has a .type property and returns action objects with that type.

Example

import { actionFactory } from '@zedux/react'

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

interface Todo {
text: string
}

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

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

You can also manually create an action factory:

const addTodo = payload => ({
type: addTodo.type,
payload,
})

addTodo.type = 'addTodo'

Definition

type ActionFactory = {
(payload?) => Action
type: string
}

Returns an action object ready to be dispatched to a store.

Passing the payload is required if a Payload generic was passed to actionFactory (this is obviously only enforced for TS users). This will be set as the payload property of the returned action object.

See Also