页面上的查询一旦导航回页

发布于 2025-01-27 16:36:41 字数 1728 浏览 3 评论 0 原文

在我的NextJS页面中,我有以下挂钩(使用 GraphQl-Codegen 生成),可获取GraphQl查询。

const { data, error, loading, fetchMore, refetch, variables } = useGetShortlistQuery({
  notifyOnNetworkStatusChange: true, // updates loading value
  defaultOptions: {
    variables: {
      offset: undefined,
      filterBy: undefined,
      sortBy: SortBy.RecentlyAdded,
      sortDirection: SortDirection.Desc,
    },
  },
});

这是 usegetShortListQuery graphql-Codegen 生成的挂钩

export function useGetShortlistQuery(
  baseOptions?: Apollo.QueryHookOptions<GetShortlistQuery, GetShortlistQueryVariables>,
) {
  const options = { ...defaultOptions, ...baseOptions };
  return Apollo.useQuery<GetShortlistQuery, GetShortlistQueryVariables>(GetShortlistDocument, options);
}

我的组件包裹在HOC中,以启用Apollo客户端

export default withApollo({ ssr: true })(Index);

withapollo hoc hoc hoc hoc使用@apollo/client 缓存 apollo客户端的属性如下。

cache: new InMemoryCache({
  typePolicies: {
    Query: {
      fields: {
        getShortlist: {
          keyArgs: [],
          merge(existing: PaginatedProperties | undefined, incoming: PaginatedProperties): PaginatedProperties {
            return {
              ...incoming,
              properties: [...(existing?.properties || []), ...(incoming?.properties || [])],
            };
          },
        },
      },
    },
  },
}),

我遇到的问题是,在此页面上,我在变量上更新 usegetShortListQuery 使用 refetch ,它依次更新>数据

但是,如果我导航到另一个页面,请使用此组件返回此页面。它似乎并没有重新启用GraphQl查询,因此返回了先前的数据。

In my nextjs page I have the following hook (generated by using graphql-codegen) that fetches a graphql query.

const { data, error, loading, fetchMore, refetch, variables } = useGetShortlistQuery({
  notifyOnNetworkStatusChange: true, // updates loading value
  defaultOptions: {
    variables: {
      offset: undefined,
      filterBy: undefined,
      sortBy: SortBy.RecentlyAdded,
      sortDirection: SortDirection.Desc,
    },
  },
});

This is the useGetShortlistQuery hook that is generated by graphql-codegen

export function useGetShortlistQuery(
  baseOptions?: Apollo.QueryHookOptions<GetShortlistQuery, GetShortlistQueryVariables>,
) {
  const options = { ...defaultOptions, ...baseOptions };
  return Apollo.useQuery<GetShortlistQuery, GetShortlistQueryVariables>(GetShortlistDocument, options);
}

my component is wrapped in a HOC to enable Apollo Client

export default withApollo({ ssr: true })(Index);

The withApollo HOC uses @apollo/client and the cache property of the apollo client is as follows.

cache: new InMemoryCache({
  typePolicies: {
    Query: {
      fields: {
        getShortlist: {
          keyArgs: [],
          merge(existing: PaginatedProperties | undefined, incoming: PaginatedProperties): PaginatedProperties {
            return {
              ...incoming,
              properties: [...(existing?.properties || []), ...(incoming?.properties || [])],
            };
          },
        },
      },
    },
  },
}),

The problem I am having is that on this page I update the variables on the useGetShortlistQuery using refetch which, in turn, updates the data.

However, if I navigate to another page, then come back to this page using this component. It doesn't seem to retrigger the graphql query so returns the previous data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿萝 2025-02-03 16:36:41

如果您使用 getstaticProps (或 getServersideProps ),则使用预渲染页面,这是已知的行为。这是由于Next.js的优化。

诀窍是要在要查看刷新的组件上拥有。您有多种方法。在组件上具有不同的告诉React应该重新呈现组件,因此会再次触发钩子。

实际示例:

export const getStaticProps: GetStaticProps = async ({ params }) => {
  const data = getData() //something that fetches your data here

  return {
    props: {
      // some other data you want to return...
      // some unique value that will be on each page
      key: data.key
    },
  }
}

const MyPage: NextPage<InferGetStaticPropsType<typeof getStaticProps>> = (props) => {
  <div key={props.key} />
}

If you are using getStaticProps (or getServerSideProps) with pre rendered pages, it is a known behavior. It is due to optimisation by Next.js not re-rendering components between page navigations, with pages like [id].js.

The trick is to have a key on components that you want to see refreshing. You have multiple ways to do so. Having a different key on components tells React that it should be re-rendering the components, and thus will trigger again the hooks.

Practical example:

export const getStaticProps: GetStaticProps = async ({ params }) => {
  const data = getData() //something that fetches your data here

  return {
    props: {
      // some other data you want to return...
      // some unique value that will be on each page
      key: data.key
    },
  }
}

const MyPage: NextPage<InferGetStaticPropsType<typeof getStaticProps>> = (props) => {
  <div key={props.key} />
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文