question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create a "actionCreatorFactory" function

See original GitHub issue

Very often I need to make reusable redux components, and also often these components state appear more than once in the Redux store.

To solve this I usually follow this pattern:

export function createParamActionCreators(paramTableName: string) {
  return {
    setParams: createActionCreator(
      `[${paramTableName}] SET_PARAMS`,
      (resolve) => (param: Param[]) => {
        return resolve(param);
      }
    ),

    addParam: createActionCreator(
      `[${paramTableName}] ADD_PARAM`,
      (resolve) => (param: Param) => {
        return resolve(param);
      }
    ),
    ...
  };
}

type ParamTableActionCreators = ReturnType<typeof createParamActionCreators>;

export function createParamTableReducer(actions: ParamTableActionCreators) {
  return createReducer([] as Param[], (handle) => [
    handle(actions.setParams, (_state, { payload }) => payload),
    handle(actions.addParam, (state, { payload }) =>
      produce(state, (draft: Draft<Param[]>) => {
        draft.push(payload);
      })
    ),
    ...
  ]);
}

I am trying to implement it myself, you can see my struggle to make a prototype here

I am opening this to see what people think about this, I think this is mostly an edge case of mine, not sure if it is worth putting on a library

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
LouizFCcommented, Oct 7, 2019

Sorry for being away. September was a busy month.

I will do a rewrite with the approach that you suggested soon as I get some time.

1reaction
LouizFCcommented, Aug 15, 2019

My workaround would be passing an object with all the string literals like so:

//Over Simplified Version

// We have a "Factory" where we declare the shape of our ActionCreators
type Factory = {
  a: (name: string) => /** ActionCreator */ ()=> { type: name };
  b: (name: string) => /** ActionCreator */ ()=> { type: name };
};

// We have a object with the same keys as the factory where we pass the ActionTypes
type Names = {
  a: "A";
  b: "B";
};

// We have produce an object with the correct ActionCreators
type Result = {
  a: () =>{ type: "A" };
  b: () =>{ type: "B" };
};

// The shape of the function
function(factory): (names) => result

It introduces some boilerplate, but the types that it produces will have the correct literals.

Read more comments on GitHub >

github_iconTop Results From Across the Web

aikoven/typescript-fsa: Type-safe action creator utilities - GitHub
actionCreatorFactory (prefix?: string, defaultIsError?: Predicate): ActionCreatorFactory. Creates Action Creator factory with optional prefix for action types.
Read more >
typescript-fsa/README.md - UNPKG
Creates Action Creator factory with optional prefix for action types. 185. 186, * `prefix?: string`: Prefix to ...
Read more >
redux-typescript-actions - npm
Async Action Creators are objects with properties started , done and failed whose values are action creators. import actionCreatorFactory ...
Read more >
Simple Action Creators for ngrx/store in Angular (2+) - Orizens
I came up with ”ActionCreatorFactory.create()” - it creates a action creator function while the payload type is defined after the ”create” ...
Read more >
Type-safe Asynchronous Actions (Redux Thunk) Using ...
Creating an asynchronous action with TypeScript FSA. Asynchronous action creators look like this: import actionCreatorFactory from ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found