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.

Add slice actions to reducer prop on Slice type

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
evertbouwcommented, Jan 22, 2020

I also have this problem. I think I’m going to use createReducer instead

const increment = createAction<number, "counter/increment">("counter/increment");
const decrement = createAction<number, "counter/decrement">("counter/decrement");

const counter = createReducer(0, builder => builder
  .addCase(increment, (state, action) => state + action.payload)
  .addCase(decrement, (state, action) => state - action.payload)
);

type MyActions = ReturnType<typeof increment | typeof decrement>;

Having to do createAction is a little more boilerplate but I don’t really see a way around it

2reactions
dorsett85commented, Jan 21, 2020

Yes 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 to AllSliceActions from the Slice type created by createSlice.

Something like:

const someSlice = createSlice({ ... });

type SomeSliceActions = typeof someSlice.actions; // this is the action creators

type AllActionTypesCombined = SomeSliceActions | SomeOtherSliceActions | ...;

or even better (which is why I mentioned having the actions put on the slice Reducer):

// rootReducer would presumably have AllActionTypesCombined
const rootReducer = combineReducers({ ... }); 

type AllActionTypesCombined = typeof rootReducer.actions // Get it from here??

or best:

export const useAppDispatch = () => useDispatch<typeof store.dispatch>();
// current type is: ThunkDispatch<CombinedState<AppState>, undefined, AnyAction> & Dispatch<AnyAction>
// would want: ThunkDispatch<CombinedState<AppState>, undefined, AllActionTypesCombined> & Dispatch<AllActionTypesCombined>

Let me know if I’m missing something easy.

Read more comments on GitHub >

github_iconTop 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 >

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