DocsAction UtilsgenerateScopedActions

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 generateScopedActions function prefixes all action types with USER/.
  • The signIn action has a type of USER/SIGN_IN.
  • The signInEffect action has a type of USER/SIGN_IN_EFFECT, along with USER/SIGN_IN_EFFECT_SUCCEEDED and USER/SIGN_IN_EFFECT_FAILED for its success and failure states.

Notes

  • createAction: Creates a standard scoped action. For example, createAction('SIGN_IN') generates an action with the type USER/SIGN_IN.
  • createEffectAction: Creates a scoped effect action. For example, createEffectAction('SIGN_IN_EFFECT') generates an effect action with types USER/SIGN_IN_EFFECT, USER/SIGN_IN_EFFECT_SUCCEEDED, and USER/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.