I'm trying to make redux dispatch action async using redux toolkit. I'm doing this to handle UI freeze caused due to redux state update (using setFilterInfo
reducer).
While searching for a way to do it, I came across createAsyncThunk. However, all the examples showed its usage in respect of fetching data. I ended up with the following code. The problem is still not solved, UI is still freezing while updating the redux state.
// This is the Thunk
export const filterInfoSetterThunk = createAsyncThunk(
"screenSlice/setFilterInfo",
async (filter, thunkAPI) =\> {
return filter;
}
);
// Slice
const screenSlice = createSlice({
name: "screenSlice",
initialState: {
filter_info: []
},
reducers: {
setFilterInfo(state, action) {
state.filter_info = action.payload.filter_info;
},
},
extraReducers: (builder) => {
builder.addCase(filterInfoSetterThunk.fulfilled, (state, action) => {
console.log("Inside extra reducer", action.payload.filter_info);
state.filter_info = action.payload.filter_info;
});
},
});
// calling filterInfoSetterThunk inside a function
updateFilterInfoInReduxStore = async (data) => {
await filterInfoSetterThunk({filter_info: data})