I have learned about redux toolkit for 2 months, and in createSlice
have reducers and extrareducers, I know they use to change state from dispatch but I don't know the difference, Where should we use they
A slice is the portion of Redux code that relates to a specific set of data and actions within the store 's state. A slice reducer is the reducer responsible for handling actions and updating the data for a given slice. This allows for smaller reducer functions that focus on a slice of state.
Reducers: As we already know, actions only tell what to do, but they don't tell how to do, so reducers are the pure functions that take the current state and action and return the new state and tell the store how to do.
Reducer is usually in a format of a switch statement, that switches between all possible Actions (Cases) and then manipulates the Store data based on action. When a reducer data changes within the redux, the properties in your components are changed and then the re-render ocurrs.
The reducers
property both creates an action creator function and responds to that action in the slice reducer. The extraReducers
allows you to respond to an action in your slice reducer but does not create an action creator function.
You will use reducers
most of the time.
You would use extraReducers
when you are dealing with an action that you have already defined somewhere else. The most common examples are responding to a createAsyncThunk
action and responding to an action from another slice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With