Create a "actionCreatorFactory" function
See original GitHub issueVery 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:
- Created 4 years ago
- Comments:8 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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.
My workaround would be passing an object with all the
string literals like so:It introduces some boilerplate, but the types that it produces will have the correct literals.