I want to know if it's possible (or a good practice) to call dispatch(someDumbAction())
from an extraReducer.
For example, I have a setData()
action in reducers
object from createSlice
.
I want to call setData()
directly in my component. But I want to call it too in a extraReducer
listener, in order to reuse the reducer logic, like below:
// Thunk Action
export const getData = createAsyncThunk('data/getData', async (params) => {
return await api.get({ params })
})
// Slice creation
const slice = createSlice({
name: 'data',
initialState: [],
reducers: {
setData: (state, { payload }) => {
state.push(payload);
})
},
extraReducers: (builder: any) => {
builder.addCase(getData.pending, (state) => {
//...
})
builder.addCase(getData.rejected, (state) => {
//...
})
builder.addCase(getData.fulfilled, (state, { payload }) => {
// Here I want to dispatch `setData` action, in order to reuse that logic
// dispatch(setData(payload));
})
},
})
// In any component: dispatch(setData([...]);