Pacote
    Preparing search index...

    Function createAction

    • Creates a typed action creator for a fixed action type.

      The returned function accepts a payload and optional metadata. Use void payloads for actions that need no payload; errors should be wrapped in a value such as Either when they are part of the action data. Metadata is stored on the meta field of the action object.

      Type Parameters

      • P = void

        Action payload type.

      • M = void

        Action metadata type.

      Parameters

      • type: string

        Stable action type string.

      Returns ActionCreator<P, M>

      An action creator carrying the supplied type property.

      import { createAction } from '@pacote/flux-actions'
      import { type Either, tryCatch } from 'fp-ts/lib/Either'

      const changeYear = createAction<number>('CHANGE_YEAR')
      changeYear(1955) // { type: 'CHANGE_YEAR', payload: 1955, meta: undefined }
      changeYear(1955, { test: true })
      // { type: 'CHANGE_YEAR', payload: 1955, meta: { test: true } }

      const changeYearWithError = createAction<Either<Error, number>>('CHANGE_YEAR')
      changeYearWithError(tryCatch(() => Number('1985'), (error) => error as Error))