使用RTK-Query 帮助.
我只是在主页上获取关注用户的帖子 它显示了所有以下用户的帖子, 但当关注用户创建新帖子时,它不是自动重新获取, 我必须手动重新加载页面,然后再进行渲染.
Issue: I have only one route for getting following users post data '/api/followingUsersPost'.
For Auto Re-Fetching it needs mutation and thats the issue here,
And in my case mutation is not in my hand, following user will update the data / dont know when. so yeah thats why i need auto re-fectch. and thats the issue How should rtk-query know that a new post has been added
// Store.js
import { configureStore } from '@reduxjs/toolkit';
import AuthSlice from './AuthSlice';
import { profileApi } from './rtk';
import { followingUsersPostApi } from './FollowingUsersPost';
const store = configureStore({
reducer: {
auth: AuthSlice.reducer,
[profileApi.reducerPath]: profileApi.reducer,
[followingUsersPostApi.reducerPath]: followingUsersPostApi.reducer,
},
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(
profileApi.middleware,
followingUsersPostApi.middleware
),
});
export default store;
// FollowingUsersPostApi.js
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
export const followingUsersPostApi = createApi({
reducerPath: 'followingUsersPostApi',
baseQuery: fetchBaseQuery({ baseUrl: '/api' }),
tagTypes: ['Post'],
endpoints: (builder) => ({
//
followingUsersPost: builder.query({
query: () => `/followingUsersPost`,
invalidatesTags: ['Post'],
}),
}),
});
export const { useFollowingUsersPostQuery } = followingUsersPostApi;
// HomePage.jsx
import { useFollowingUsersPostQuery } from '../../Store/FollowingUsersPost';
const HomePage = () => {
const { data, isLoading, error } = useFollowingUsersPostQuery();
}
我查过doctor 了,有providesTags和invalidatesTags 但我只有一条路可以让粉丝发帖, 所以我用了invalidatesTags