DocsAction UtilscreateEffectAction

createEffectAction

createEffectAction is a utility for creating Redux actions designed to handle asynchronous workflows. Each action carries two additional action creators: .succeeded and .failed, which are used to represent the success and failure states of an effect. It is intended to be used with the effect operator in epics.


Importing

To use createEffectAction, import it from @blue-functor/remodel:

import { createEffectAction } from '@blue-functor/remodel';

Example Usage

src/models/users/actions.ts
import { createEffectAction } from '@blue-functor/remodel';
 
// Define an effect action
export const signIn = createEffectAction<
  { username: string; password: string }, // Payload
  { token: string; user: { username: string } }, // SuccessPayload
  { error: string } // FailurePayload
>('USER/SIGN_IN');

In this example:

  • signIn is an effect action for the USER/SIGN_IN type.
  • signIn.succeeded is triggered when the effect completes successfully and dispatches USER/SIGN_IN_SUCCEEDED with the success payload.
  • signIn.failed is triggered when the effect fails and dispatches USER/SIGN_IN_FAILED with the failure payload.

Notes

  • The main action creator (signIn) is used to initiate the effect.
  • succeeded: Dispatch this when the effect completes successfully. It includes the success payload and an optional meta field with the initial payload.
  • failed: Dispatch this when the effect fails. It includes the failure payload and an optional meta field with the initial payload.
  • The createEffectAction utility integrates seamlessly with the effect operator in epics, simplifying asynchronous logic handling and making success and failure states explicit.