要重置查询状态的Redux Toolkit工具,您可以使用createSlice函数来创建一个Slice,并在reducer中定义一个重置查询状态的action。以下是一个代码示例:
import { createSlice } from '@reduxjs/toolkit';
const initialState = {
isLoading: false,
error: null,
data: null,
};
const querySlice = createSlice({
name: 'query',
initialState,
reducers: {
startLoading: (state) => {
state.isLoading = true;
state.error = null;
state.data = null;
},
querySuccess: (state, action) => {
state.isLoading = false;
state.error = null;
state.data = action.payload;
},
queryError: (state, action) => {
state.isLoading = false;
state.error = action.payload;
state.data = null;
},
resetQuery: () => initialState,
},
});
export const { startLoading, querySuccess, queryError, resetQuery } = querySlice.actions;
export default querySlice.reducer;
在上面的代码中,我们创建了一个名为query的Slice,并定义了四个reducer:startLoading、querySuccess、queryError和resetQuery。resetQuery reducer将查询状态重置为初始状态,即将isLoading、error和data置为初始值。您可以在需要重置查询状态的地方,调用dispatch(resetQuery())来触发这个action。
请注意,上述代码中的initialState是一个示例,您可以根据您的需求定义自己的初始状态。