Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between reducers and extrareducers in redux toolkit?

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

like image 282
kalipts Avatar asked Mar 01 '21 16:03

kalipts


People also ask

What is the difference between Slice and reducer?

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.

What is the difference between reducers and actions?

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.

What is the difference between reducer and Redux?

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.


1 Answers

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.

like image 178
Linda Paiste Avatar answered Oct 21 '22 18:10

Linda Paiste