我正在我的一个项目中测试 rtk-query 并且有一个问题我不明白什么时候网站加载第一次查询工作并且接收到数据但是当我尝试更新或创建我在 redux 开发工具中获得的任何内容时突变状态被拒绝,但是当我刷新网站并再次尝试时,它会起作用,因此仅当网站第一次加载时才会出现问题,这是我的代码
// import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query';
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
const auth = JSON.parse(localStorage.getItem('auth'));
export const crudApi = createApi({
baseQuery: fetchBaseQuery({
baseUrl: import.meta.env.VITE_APP_API,
// prepareHeaders: (headers) => {
// console.log(headers);
// },
}),
endpoints: (builder) => ({
getList: builder.query({
query: (path) => ({
url: `${path}/list`,
}),
}),
checkCopon: builder.mutation({
query: (code) => ({
url: `copon/check`,
body: { code },
method: 'POST',
}),
}),
filterDishes: builder.mutation({
query: (tags) => ({
url: `dish/filter`,
body: { tags },
method: 'POST',
}),
}),
getTodaysOrders: builder.query({
query: () => ({
url: `order/todayList`,
body: { today: true },
method: 'POST',
}),
}),
getSingle: builder.query({
query: ({ path, id }) => {
console.log(path, id);
return {
url: `${path}/get`,
body: { id },
method: 'POST',
};
},
}),
listOrderByUser: builder.query({
query: (id) => {
console.log(id);
return {
url: `order/listByUser`,
body: { id },
method: 'POST',
};
},
}),
docUpdate: builder.mutation({
query: (body) => {
console.log(body);
return {
url: `${body.path}/update`,
body: body,
method: 'POST',
headers: {
Authorization: 'Bearer ' + auth.token,
},
};
},
}),
Add: builder.mutation({
query: (body) => {
console.log(body);
return {
url: `${body.path}/add`,
body: body,
method: 'POST',
headers: {
Authorization: 'Bearer ' + auth.token,
},
};
},
}),
Delete: builder.mutation({
query: (body) => {
console.log(body);
console.log(`${body.path}/delete`);
return {
url: `${body.path}/delete`,
body: { ids: body.ids },
method: 'POST',
};
},
}),
}),
});
// Export hooks for usage in functional components
export const {
useListOrderByUserQuery,
useGetListQuery,
useGetSingleQuery,
useDocUpdateMutation,
useDeleteMutation,
useAddMutation,
useGetTodaysOrdersQuery,
useCheckCoponMutation,
useFilterDishesMutation,
} = crudApi;
如果有人可以帮助我不胜感激,我不想重写所有代码来修复它