Pending Manual Refetch Is Cancelled On Component Unmount
Introduction
In the world of TanStack Query, managing data fetching and caching is a crucial aspect of building robust and efficient applications. However, a recent issue has been reported where a pending manual refetch is cancelled when a component is unmounted. This article delves into the details of this bug, its reproducible example, and the expected behavior.
Describe the Bug
The bug occurs when a query in a component has refetchOnMount: false
set, indicating that the data should not be refetched when the component is mounted. However, in another component, the same query is manually refetched under certain conditions. If the component with the manual refetch is navigated to while the refetch is in progress, the in-flight request is cancelled.
Your Minimal, Reproducible Example
A minimal, reproducible example of this bug can be found in the following Codesandbox link: https://codesandbox.io/p/sandbox/weathered-hooks-qd3v7w
Steps to Reproduce
To reproduce this bug, follow these steps:
- Navigate to Component A. The mock expensive call is made as expected and resolves after 3 seconds.
- Navigate to Component B and push the button "Refetch Query".
- Within the 3 seconds, navigate to Component A, then back to Component B.
- The pending manual refetch fired from Component B is cancelled.
Expected Behavior
The expected behavior is that the manual refetch fired from Component B should not be cancelled or impacted by Component A's mount lifetime. However, the current implementation cancels the refetch when Component A is unmounted.
Why Do I Expect This Behavior?
There are several reasons why I expect this behavior:
- Query Ownership: The query could also be used in other components. If Component A's unmounting cancels the explicit refetch fired elsewhere, that means other components will also keep having stale data when they mount.
- Data Freshness: The next time Component A is mounted, it will have stale data. This is because the refetch was cancelled, and the data was not updated.
- Semantic Consistency: It doesn't semantically make sense to me that Component A's lifetime should own a query fired outside of it. The query should be independent of the component's lifetime.
How Often Does This Bug Happen?
This bug does not happen frequently, as it requires a specific sequence of events to occur.
Screenshots or Videos
No screenshots or videos are provided as this is a text-based issue.
Platform
The bug is reported on the following platform:
- OS: Windows
- Browser: Edge
- Version: 136.0.3240.64
TanStack Query Adapter
The TanStack Query adapter used is react-query
.
TanStack Query Version
The version of TanStack Query used is 5.76.1
.
TypeScript Version
The version of TypeScript used is 4.4.4
.
Additional Context
No additional context is provided.
Conclusion
Q: What is the issue with the pending manual refetch being cancelled on component unmount?
A: The issue is that the manual refetch is cancelled when a component is unmounted, which can lead to stale data and inconsistent behavior. This is because the query is tied to the component's lifetime, rather than being independent.
Q: Why does this issue occur?
A: This issue occurs because of the way TanStack Query handles queries and their lifetimes. When a component is unmounted, its queries are also cancelled, which can lead to the manual refetch being cancelled.
Q: What are the implications of this issue?
A: The implications of this issue are significant. If the manual refetch is cancelled, the data will not be updated, and the component will display stale data. This can lead to inconsistent behavior and a poor user experience.
Q: How can I reproduce this issue?
A: To reproduce this issue, you can follow the steps outlined in the reproducible example. Navigate to Component A, then to Component B, and push the button "Refetch Query". Within the 3 seconds, navigate to Component A, then back to Component B. The pending manual refetch fired from Component B will be cancelled.
Q: What is the expected behavior?
A: The expected behavior is that the manual refetch should not be cancelled by the component's mount lifetime. The query should be independent of the component's lifetime and should continue to run even if the component is unmounted.
Q: Why is this issue not happening frequently?
A: This issue is not happening frequently because it requires a specific sequence of events to occur. The component must be unmounted while the manual refetch is in progress, which is not a common scenario.
Q: What are the possible solutions to this issue?
A: There are several possible solutions to this issue. One solution is to use a different query strategy, such as using a separate query for the manual refetch. Another solution is to use a library that provides more control over query lifetimes, such as react-query
.
Q: How can I prevent this issue from occurring in the future?
A: To prevent this issue from occurring in the future, you can follow best practices for query management, such as using a separate query for the manual refetch and ensuring that the query is independent of the component's lifetime.
Q: What are the benefits of using TanStack Query?
A: TanStack Query provides several benefits, including automatic caching, query optimization, and error handling. However, it also has some limitations, such as the issue with pending manual refetch being cancelled on component unmount.
Q: How can I get help with TanStack Query?
A: You can get help with TanStack Query by visiting the official documentation, joining the community forums, or reaching out to the maintainers directly.