Add slice actions to reducer prop on Slice type
See original GitHub issueThe return value of createSlice
is a Slice
type that has a reducer prop with the original State passed in to Reducer
.
export declare interface Slice<State = any, CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>> {
/**
* The slice name.
*/
name: string;
/**
* The slice's reducer.
*/
reducer: Reducer<State>; // Why not Reducer<State, SliceAction>??
/**
* Action creators for the types of actions that are handled by the slice
* reducer.
*/
actions: CaseReducerActions<CaseReducers>;
/**
* The individual case reducer functions that were passed in the `reducers` parameter.
* This enables reuse and testing if they were defined inline when calling `createSlice`.
*/
caseReducers: SliceDefinedCaseReducers<CaseReducers>;
}
Can we have the second argument to Reducer
passed in as well? Otherwise this reducer (and any others pass into configureStore
will mean store.dispatch
will accept AnyAction
.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:14 (5 by maintainers)
Top Results From Across the Web
createSlice - Redux Toolkit
A function that accepts an initial state, an object of reducer functions, and a "slice name", and automatically generates action creators and ...
Read more >How To Setup Redux Slices with Redux Toolkit - SoftKraft
createSlice takes an object of reducer functions, a slice name, and an initial state value and lets us auto-generate action types and action...
Read more >How to create a strongly typed generic function for redux ...
1 Answer 1 · You must infer the type of reducers with a generic type · You must use the generic reducers type...
Read more >A powerful React + Redux Toolkit pattern (reuseable state ...
It utilises all features of Redux Toolkit to provide reusable slices, actions, and reducers. · Each slice is completely independent, however it ...
Read more >How to use redux toolkit with using slice, react, typescript(First ...
A function that accepts an initial state, an object of reducer functions, and a "slice name", and automatically generates action creators and ...
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
I also have this problem. I think I’m going to use createReducer instead
Having to do
createAction
is a little more boilerplate but I don’t really see a way around itYes exactly, sorry if I was unclear.
I have a custom hook:
const useAppDispatch = () => useDispatch<AllActionTypesCombined>();
It’s easy to have
AllActionTypesCombined
with context api and/or redux as it’s part of my types already, but it would be nice to have access toAllSliceActions
from theSlice
type created bycreateSlice
.Something like:
or even better (which is why I mentioned having the actions put on the slice
Reducer
):or best:
Let me know if I’m missing something easy.