Only fetch data once if component is open - after close and reopen fetch again
Hey guys, if I open a sidebar component this will trigger a fetch of an API and load some data which takes a lot of power in the backend because its a calculation. After the successful fetch the data will stay in fresh and if I change something in the UI it will trigger a refetch and it will do the calculation again in the backend. I dont want that this calculation runs again so I don't want a fetch of this data, it should only show the cached data even if something of the query key changes. If the component closes and opens again it should fetch it again. What is the best way to do that? I am on react-query v ^3.38.0.
Short: I only want that component to fetch once when opened. Refetch should be only triggered if component closes and reopens.
Ideas:
Should I use removeQueries or cancel queries?
enabled: false
Should I use the enabled flag on the fetch and set the refetch on the button to open the component?
1 Reply
ambitious-aqua•4y ago
i would say enabled:false but it's a pretty weird case I have to say 😅