使用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 了,有providesTagsinvalidatesTags 但我只有一条路可以让粉丝发帖, 所以我用了invalidatesTags

推荐答案

RTK-Query应该如何知道添加了新的帖子? 我没有看到你增加一个新帖子的变化.请看mutations documentations,然后看看cache在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 });

Reactjs相关问答推荐

有正确的方法来设置我的金牛座应用程序的图标吗?

在Reaction中测试条件组件

如何在取数后添加新数据,并在页面上保存旧数据?

如何将图像(在代码中称为自己)定位在蓝色圈内的点上?我正在使用material UI和Reaction

防止在Reaction中卸载

如何在单击行中的图标时避免选中ionic 复选框?

在动态React组件中删除事件侦听器

如何解决Reaction中遗留的上下文API错误?

无法在REACTION TANSTACK查询中传递参数

阻止组件更新的多分派Redux Thunk或setState是否有任何问题

React-chartjs-2:红色和绿色之间的差距

tRPC/react-query,在所有查询完成之前,组件不会收到任何 useQuery 的结果

我可以在加载器函数中多次 fetch() 并使用一次 useloaderdata() 提取它吗?

为什么我的 Next.js (React) 组件只有在页面中时才有效?

如何初始化 redux 全局存储 preloadedState

Suspense/Await - 解析数据加载/保存到状态后无限循环

NextJS htaccess 设置

如何在react 中更新子组件中的变量?

调用 Jest 和服务方法的问题

如何在react 日历中自定义带有圆形边框的日期项?