Apollo UseQuery并不总是触发功能组件的重新渲染
这是我的功能组件,使用useQuery
来自@apollo/client
:
function MyComponent(props) {
const { data } = useQuery<MarketScannerSearchProps>(
MARKET_SCANNER_SEARCH_PROPS_QUERY,
{
onCompleted: (data) => {
console.log("completed", data)
},
}
)
console.log("data", data)
// ... rest of function + return rendered result
}
我在控制台中获得此功能:
data undefined
data undefined
completed {marketScannerSearchProps: {…}}
为什么没有data {MarketScannerSearchProps: {…}}
最后?
这并不总是可再现的,只发生一次我的应用程序每5个重新加载一次。这就是它工作时的外观:
data undefined
data {marketScannerSearchProps: {…}}
completed {marketScannerSearchProps: {…}}
因此,将组件的第二次渲染到其中,但并非总是如此,如果数据不在第二播放的情况下,则它将永远不会更新它。
查询完成后,Apollo不应该每次触发重新渲染吗?我在这里想念什么?
编辑:我认为这个问题现在可能已经消失了。我关闭并打开了几次Chrome Dev工具,现在似乎已经消失了。难道是阿波罗客户端缓存搞砸了吗?
This is my functional component that makes use of useQuery
from @apollo/client
:
function MyComponent(props) {
const { data } = useQuery<MarketScannerSearchProps>(
MARKET_SCANNER_SEARCH_PROPS_QUERY,
{
onCompleted: (data) => {
console.log("completed", data)
},
}
)
console.log("data", data)
// ... rest of function + return rendered result
}
I get this in my console:
data undefined
data undefined
completed {marketScannerSearchProps: {…}}
why isn't there a data {marketScannerSearchProps: {…}}
at the end?
This is not always reproducible, only happens every 5th reload or so of my app. This is how it looks when it works:
data undefined
data {marketScannerSearchProps: {…}}
completed {marketScannerSearchProps: {…}}
So somehow the 2nd time the component is rendered the data is there, but not always, and if the data isn't there by the 2nd render, it never updates it afterwards.
Shouldn't apollo trigger a re-render every time once the query completes? What am I missing here?
EDIT: I think this issue might be gone now. I closed and opened Chrome Dev Tools a few times, and now it seems to be gone. Could it be Apollo client side caching getting messed up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有同样的问题。
这是React Dev工具的一个问题(我已经尝试了逐步删除各种镀铬扩展,并一直发现React Dev工具是罪魁祸首)。 目前最好的解决方案是禁用React Dev工具扩展,关闭Chrome Dev工具,重新加载页面,然后所有内容都可以使用。,
因为这是React Dev Tools的问题,我'll在其存储库中创建一个github问题,并使用指向它的链接更新此答案,以便将来任何人都可以找到它。
I'm having the same issue.
It's an issue with React Dev Tools (I've experimented with incrementally removing various chrome extensions and consistently found that React Dev Tools is the culprit). The best solution, for now, is to disable the React Dev Tools extension, close the Chrome Dev Tools, reload the page, and then everything will work.
Since this is an issue with React Dev Tools, I'll create a GitHub issue on their repo and update this answer with a link to it so anyone in the future can find it.