I'm new to React and I'm learning to use React to build a web app. I found Redux Toolkit useful and use its createSlice() function to implement the basic features. However, I encountered a "best practice"-related problem and I'm not sure whether I've built the architecture of the app correctly.
Let's say I have a user object stored in Redux. I created an async thunk function to fetch related information:
export const getUserInfo = createAsyncThunk('user/get', async (userId, thunkApi) => {
// fetching information using api
}
Correspondingly, I handled the pending/fulfilled/rejected callback as follows:
const userSlice = createSlice({
name: 'user',
initialState,
reducers: {
setShowProgress(state, action: PayloadAction<boolean>) {
state.showProgress = action.payload;
},
clearError(state) {
state.error = null;
state.errorMessage = null;
}
},
extraReducers: builder => {
builder.addCase(getUserInfo.pending, (state, action) => {
// My question is here >_<
}
builder.addCase(getUserInfo.fulfilled, (state, action) => {
// handle data assignments
})
builder.addCase(getUserInfo.rejected, (state, action) => {
// handle error messages
})
}
})
Considering modifying display status flags are quite common in other feature api implementations, I wrapped the two functions (setShowProgress() and clearError()) in reducers. Here comes my question: How can I reference the two functions in getUserInfo.pending function?
Though I could just assign the showProgress and error state variables in getUserInfo.pending instead of trying to call the reducer functions, that will definitely introduce duplicate code when I implement other fetching actions in the future. If it is not the recommended pattern, what is the best practice for this scenario?