Mosh Redux part 6 (reduxjs toolkit’s slice)

redux toolkit also has slices, which combines createAction and createReducer.

So it automatically creates actions and reducers for us.

questions.js
let’s import createSlice

createSlice takes an object with properties name, initialState, and reducers.
name represent the feature of this reducer.
intialState is the starting point of the state.
Finally, we have a reducers property, which references an object. Inside this object,

In the previous approach, questionAdded was an object, so we can use [questionAdded] to check for the type.

In this approach, questionAdded is just a property. Slices automatically creates actionTypes and actions for us.

let’s look at this slice object.

We see the properties. We need to default export the reducer and export the actions.

So with this implementation we can remove the previous actions and reducers.

also need to export actions. We do so by extracting actions from slice.actions