简体   繁体   English

react-query,refetch 不会触发 useEffect

[英]react-query, refetch don't trigger useEffect

I want to refetch the data after save is succeeded.保存成功后我想refetch数据。

here is the demo that demonstrates the issue -这是演示该问题的演示 -

https://codesandbox.io/s/romantic-orla-h9scuc?file=/src/index.js https://codesandbox.io/s/romantic-orla-h9scuc?file=/src/index.js

Issue here is useEffect not gets triggered after data changes.这里的问题是data更改后不会触发 useEffect。 So the log from line 111 never happens...所以第111行的日志永远不会发生......

Is this behaviour expected??这是预期的行为吗? If not What am I doing wrong??如果不是我做错了什么?

Also If this behaviour is expected then how to get the latest data programmatically?此外,如果这种行为是预期的,那么如何以编程方式获取最新数据?

I don't know much about React-Query but in RTK Query after using refetch , for this kind of behavior, you should pass isFetching to useEffect .我对 React-Query 了解不多,但是在使用refetch后的 RTK Query 中,对于这种行为,您应该将isFetching传递给useEffect This works like a charm and useEffect runs again when refetching is done.这就像一个魅力和useEffect在重新获取完成后再次运行。

That happens because you already re-fetch the same data with the same reference because react-query gets it from the cache, So according to useEffect the data doesn't change at all, and will not re-execute.发生这种情况是因为您已经使用相同的引用重新获取相同的数据,因为 react-query 从缓存中获取它,因此根据useEffect data根本不会改变,也不会重新执行。

So, That's the expected behavior.所以,这是预期的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM