页面上的查询一旦导航回页
在我的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查询,因此返回了先前的数据。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用
getstaticProps
(或getServersideProps
),则使用预渲染页面,这是已知的行为。这是由于Next.js的优化。诀窍是要在要查看刷新的组件上拥有
键
。您有多种方法。在组件上具有不同的键
告诉React应该重新呈现组件,因此会再次触发钩子。实际示例:
If you are using
getStaticProps
(orgetServerSideProps
) 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 differentkey
on components tells React that it should be re-rendering the components, and thus will trigger again the hooks.Practical example: