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:
signInis an effect action for theUSER/SIGN_INtype.signIn.succeededis triggered when the effect completes successfully and dispatchesUSER/SIGN_IN_SUCCEEDEDwith the success payload.signIn.failedis triggered when the effect fails and dispatchesUSER/SIGN_IN_FAILEDwith 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 optionalmetafield with the initial payload.failed: Dispatch this when the effect fails. It includes the failure payload and an optionalmetafield with the initial payload.- The
createEffectActionutility integrates seamlessly with theeffectoperator in epics, simplifying asynchronous logic handling and making success and failure states explicit.