-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat: add placeholderData to queryObserver #1161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/tannerlinsley/react-query/27a9g3ka3 |
Great to see you are digging into it! I kind of like the
const { data = 'initial' } = useQuery()
|
1
2
return query.isLoading ? 'Loading' : query.isError ? 'Error' : 'Show Data JSX'
// Becomes
return query.isPlaceholder
? 'Show Successful JSX'
: query.isLoading
? 'Loading'
: query.isError
? 'Error'
: 'Show Successful JSX' Empirically, I can see how adding another state would make technical sense, but where successful and placeholder states are (and should be) almost identical in templating logic, I see more negatives (breaking everyone's templates who uses it) with introducing the new state. |
Yeah a const { data } = useQuery()
if (data) {
return <div>{data}</div>
} If you also want to know what kind of data it is: const { data, isPlaceholderData } = useQuery()
if (data) {
return <div className={isPlaceholderData ? 'loading' : ''}>{data}</div>
} |
Hmmm. That would create a lot of upheaval from the current documentation, but if it's the right thing to do, we should discuss it thoroughly to make sure we're making the right move. It would be a relatively large change conceptually from what it's been in the past. |
I think the whole data checking approach fits this situation pretty well, but it's when we get into showing/persisting errors and data that the approach rubs me the wrong way. |
From a maintainer perspective, it costs us very little to just keep things the way they are and add the feature. It doesn't break anyone in v2 or v3 and it's pretty clear what the option is doing in the docs. We can always reformat things in the future, but my guess is that no one would even bat an eyelash at the placeholder/isSuccess combo for quite a long time. In contrast, changing everything to be data/error/loading checking as a primary means of templating would be a ton of work today and probably create a lot of churn for little payoff IMO. |
Yeah I agree let's keep the value/status checking discussion in #1108. It should not be blocking this feature. Wouldn't a |
Added |
I'm guessing this will need to be manually added to v3 as well. |
🎉 This PR is included in version 3.2.0-beta.4 🎉 The release is available on: Your semantic-release bot 📦🚀 |
It's not clear from the docs if setting placeholderData means the hook won't suspend even when isLoading and suspense are both true. |
Trying my hand at a bit of Typescript! I feel so out of my element here... but hopefully not for long. 😉
This PR is clearly incomplete, but actually works as a solution in my codesandbox from #1158.
Thoughts here?