Using RTK-Query Help.
I'm fetching only following users post on HomePage Its showing all following users post, but when following user creates new post, its not Auto Re-Fetching, I have to reload the page manually then its rendering.
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();
}
I checked docs but there is providesTags and invalidatesTags but i have only one route for getting followers post, so i have use invalidatesTags
CodePudding user response:
How should rtk-query know that a new post has been added? I don't see a mutation in which you add a new post. Please see mutations documentations, and after that see how cache works in rtk-query.
Polling
If you can't handle the mutation, I suppose the interval updates your only way.
Read more about polling
const state = useFollowingUsersPostQuery(undefined, { pollingInterval: 3000 });