Omit running select when placeholderData is returned and placeholderData: keepPreviousData is set #8998
-
I want to omit running select when According to the docs:
From the point of a query, which key ( But from the point of a reusable query hook consumer, it's not very intuitive (for me): const transformData = (data) => ({
...data, // changes object instance
number: data.number * 2,
});
const useGetTasks = (filters) => useQuery({
...
placeholderData: keepPreviousData, // or any other function that doesn't mutate previous data
select: transformData, // no new references to function
})
cosnt { data, isPlaceholderData } = useGetTasks(filters);
useMemo(() => {
// After filters has changed and isPlaceholderData is true, this function will rerun,
// but, while in load state, the data returned from hook hasn't changed yet, it just returns new instance created by select
// I would like to rerun useMemo only when there is new data after a successful fetch
}, [data]); Or maybe there is a better approach for this use case? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
sorry that doesn’t make much sense to me. Consider the following example:
This component fetches a post, selects the title and renders it as upper cased. When you transition between ids, it will keep the title of the previous query and transition to the new title once new data comes in. If we don’t run select when
I think that should just work in the example code you’ve shown. Please show a stackblitz or codesandbox reproduction that shows that this
I think you are right that we probably shouldn’t re-run query/packages/query-core/src/queryObserver.ts Lines 527 to 529 in 1b54538 I think this would do it:
but only if you return exactly previousData from the Also, side question: You can do what you do inside |
Beta Was this translation helpful? Give feedback.
-
@TkDodo Thanks for the quick reply! Here is StackBlitz reproduction There is a select function with commented |
Beta Was this translation helpful? Give feedback.
yeah sorry it seems like we have to run select at least once for the new query, even if you have placeholderData and a stable select.