It would be nice to have setPlaceholderData that works same as setQueryData but for placeholderData. I wanna use it like this:
Once a user navigates from the listing to the detail page, the setPlaceholderData is used to set data from the listing as placeholderData for the opened detail page.
I wrote an article about such optimization. setPlaceholderData method would make implementation much cleaner
https://akhmadshin.dev/blog/boost-web-performance-with-client-side-prediction
#Why is there no setPlaceholderData method?
6 messages · Page 1 of 1 (latest)
There is. You don't push placeholder data into query, you pull it from the cache of another query.
Wow, that's a good solution! Thanks a lot
That's exactly why they don't have setPlaceholderData(push approach). The pull approach is cleaner, requires less code, and is centralized. You write it once, and it works every time. Unlike initialData the placeholderData doesn't change the query behaviour, so it's ok to always use the pull approach.
yeah the key to understand is that placeholderData doesn't go into the cache, it works on QueryObserver level, and the observer is created by useQuery, so you can't "set it" from somewhere else.
see: https://tkdodo.eu/blog/placeholder-and-initial-data-in-react-query