generateScopedActions
generateScopedActions is a utility for creating scoped action creators. It simplifies action creation by prefixing all action types with a specified scope, ensuring consistent namespacing across your application.
Importing
To use generateScopedActions, import it from @blue-functor/remodel:
import { generateScopedActions } from '@blue-functor/remodel';Example Usage
src/models/users/actions.ts
import { generateScopedActions } from '@blue-functor/remodel';
const { createAction, createEffectAction } = generateScopedActions('USER');
// Define scoped actions
export const signIn = createAction<{ username: string; password: string }>('SIGN_IN');
export const signInEffect = createEffectAction<
{ username: string; password: string },
{ token: string; user: { username: string } },
{ error: string }
>('SIGN_IN_EFFECT');In this example:
- The
generateScopedActionsfunction prefixes all action types withUSER/. - The
signInaction has a type ofUSER/SIGN_IN. - The
signInEffectaction has a type ofUSER/SIGN_IN_EFFECT, along withUSER/SIGN_IN_EFFECT_SUCCEEDEDandUSER/SIGN_IN_EFFECT_FAILEDfor its success and failure states.
Notes
createAction: Creates a standard scoped action. For example,createAction('SIGN_IN')generates an action with the typeUSER/SIGN_IN.createEffectAction: Creates a scoped effect action. For example,createEffectAction('SIGN_IN_EFFECT')generates an effect action with typesUSER/SIGN_IN_EFFECT,USER/SIGN_IN_EFFECT_SUCCEEDED, andUSER/SIGN_IN_EFFECT_FAILED.- This utility is especially useful in modularized applications where action type collisions might occur. The scoping ensures all actions are uniquely identifiable while maintaining a clear and consistent naming pattern.