-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
docs(useQuery): Write a story that visually shows useQuery loading/error/success states #73600
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,343 @@ | ||
import {Fragment, type ReactNode, Suspense, useEffect, useState} from 'react'; | ||
import {useQuery} from '@tanstack/react-query'; | ||
|
||
import {Button} from 'sentry/components/button'; | ||
import ButtonBar from 'sentry/components/buttonBar'; | ||
import {Flex} from 'sentry/components/container/flex'; | ||
import ObjectInspector from 'sentry/components/objectInspector'; | ||
import SideBySide from 'sentry/components/stories/sideBySide'; | ||
import SizingWindow from 'sentry/components/stories/sizingWindow'; | ||
import storyBook from 'sentry/stories/storyBook'; | ||
import {space} from 'sentry/styles/space'; | ||
import {useQueryClient, type UseQueryOptions} from 'sentry/utils/queryClient'; | ||
|
||
/** | ||
* Fake endpoint to simulate loading data with 5 second delay | ||
*/ | ||
function fetchData() { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve({some: 'data'}); | ||
}, 5_000); | ||
}); | ||
} | ||
|
||
function fetchThrowsError() { | ||
return new Promise((_, reject) => { | ||
setTimeout(() => { | ||
reject('An error happened'); | ||
}, 5_000); | ||
}); | ||
} | ||
|
||
/** | ||
* Helper react component to track how long something has been rendered | ||
*/ | ||
function LoadingFallback() { | ||
const [seconds, setSeconds] = useState(0); | ||
useEffect(() => { | ||
const interval = setInterval(() => setSeconds(prev => prev + 1), 1_000); | ||
return () => clearInterval(interval); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
Loading... | ||
<p>Rendered for {seconds} seconds</p> | ||
</div> | ||
); | ||
} | ||
|
||
function ToggleMounted({ | ||
children, | ||
queryKeys, | ||
}: { | ||
children: ReactNode; | ||
queryKeys: Array<readonly unknown[]>; | ||
}) { | ||
const [isMounted, setIsMounted] = useState(true); | ||
const queryClient = useQueryClient(); | ||
|
||
return ( | ||
<Fragment> | ||
<Flex align="flex-start" column gap={space(1)}> | ||
<ButtonBar merged> | ||
<Button size="sm" onClick={() => setIsMounted(prev => !prev)}> | ||
{isMounted ? 'Unmount' : 'Mount'} | ||
</Button> | ||
<Button | ||
size="sm" | ||
onClick={() => | ||
queryKeys.forEach(queryKey => | ||
queryClient.resetQueries({queryKey, exact: true}) | ||
) | ||
} | ||
> | ||
Reset Queries | ||
</Button> | ||
</ButtonBar> | ||
<SizingWindow>{isMounted ? children : null}</SizingWindow> | ||
</Flex> | ||
</Fragment> | ||
); | ||
} | ||
|
||
function DataContainer({ | ||
children, | ||
options, | ||
}: { | ||
options: UseQueryOptions; | ||
children?: ReactNode; | ||
}) { | ||
const { | ||
data, | ||
error, | ||
isError, | ||
isFetching, | ||
isInitialLoading, | ||
isLoading, | ||
isSuccess, | ||
refetch, | ||
status, | ||
} = useQuery(options); | ||
|
||
return ( | ||
<Flex column gap={space(1)}> | ||
{children} | ||
<Button size="xs" onClick={() => refetch()}> | ||
refetch | ||
</Button> | ||
<ObjectInspector | ||
data={{ | ||
status, | ||
isFetching, | ||
isLoading, | ||
isInitialLoading, | ||
isError, | ||
error, | ||
isSuccess, | ||
data, | ||
}} | ||
expandLevel={2} | ||
/> | ||
</Flex> | ||
); | ||
} | ||
|
||
export default storyBook('useQuery', story => { | ||
story('README', () => ( | ||
<Fragment> | ||
<p> | ||
This is a set of examples for how to call <code>useQuery()</code> and make the | ||
most of it's built-in fetching/loading/error/success states. | ||
</p> | ||
<p> | ||
These examples import from <code>@tanstack/react-query</code> directly, instead of | ||
from <code>sentry/utils/queryClient</code> in order to directly test the api | ||
without sentry specific helpers getting in the way. What's being tested are the | ||
return types, which are consistent between <code>sentry/utils/queryClient</code>{' '} | ||
and <code>@tanstack/react-query</code>. | ||
</p> | ||
<p> | ||
You should prefer to import from <code>sentry/utils/queryClient</code> as much as | ||
possible to benefit from easier and consistent data fetching within sentry. | ||
</p> | ||
</Fragment> | ||
)); | ||
story('TL/DR', () => ( | ||
<Fragment> | ||
<p> | ||
It seems like you can get really far by checking BOTH{' '} | ||
<code>isFetching || isLoading</code> when you are waiting for the first render, | ||
particularly when using <code>enabled</code> but not setting{' '} | ||
<code>initialData</code> (which is a common situation). | ||
</p> | ||
<p> | ||
Also using <code>suspense</code> is a pattern that we should adopt more because it | ||
simplifies a lot of state coalescing, especially when multiple fetches are | ||
happening concurrently. | ||
</p> | ||
</Fragment> | ||
)); | ||
|
||
story('Basic', () => { | ||
return ( | ||
<ToggleMounted queryKeys={[['story-basic-success'], ['story-basic-error']]}> | ||
<SideBySide> | ||
<DataContainer | ||
options={{ | ||
queryKey: ['story-basic-success'], | ||
queryFn: fetchData, | ||
}} | ||
> | ||
<div>status begins as "loading"</div> | ||
</DataContainer> | ||
<DataContainer | ||
options={{ | ||
queryKey: ['story-basic-error'], | ||
queryFn: fetchThrowsError, | ||
retry: 0, | ||
}} | ||
> | ||
<div>expected to throw an error</div> | ||
</DataContainer> | ||
</SideBySide> | ||
</ToggleMounted> | ||
); | ||
}); | ||
|
||
story('enabled: false', () => { | ||
return ( | ||
<ToggleMounted queryKeys={[['story-disabled-success'], ['story-disabled-error']]}> | ||
<SideBySide> | ||
<DataContainer | ||
options={{ | ||
queryKey: ['story-disabled-success'], | ||
queryFn: fetchData, | ||
enabled: false, | ||
}} | ||
> | ||
<div> | ||
manual refetch ignores <code>enabled: false</code> | ||
</div> | ||
</DataContainer> | ||
<DataContainer | ||
options={{ | ||
queryKey: ['story-disabled-error'], | ||
queryFn: fetchThrowsError, | ||
retry: 0, | ||
enabled: false, | ||
}} | ||
> | ||
<div> | ||
manual refetch ignores <code>enabled: false</code> | ||
</div> | ||
</DataContainer> | ||
</SideBySide> | ||
</ToggleMounted> | ||
); | ||
}); | ||
|
||
story('initialData: {...}', () => { | ||
return ( | ||
<ToggleMounted | ||
queryKeys={[['story-initialData-success'], ['story-initialData-error']]} | ||
> | ||
<SideBySide> | ||
<DataContainer | ||
options={{ | ||
queryKey: ['story-initialData-success'], | ||
queryFn: fetchData, | ||
initialData: {tmp: 'data'}, | ||
}} | ||
> | ||
<div>status begins as "success"</div> | ||
</DataContainer> | ||
<DataContainer | ||
options={{ | ||
queryKey: ['story-initialData-error'], | ||
queryFn: fetchThrowsError, | ||
retry: 0, | ||
initialData: {tmp: 'data'}, | ||
}} | ||
> | ||
<div>expected to throw an error</div> | ||
</DataContainer> | ||
</SideBySide> | ||
</ToggleMounted> | ||
); | ||
}); | ||
|
||
story('enabled:false, initialData: {...}', () => { | ||
return ( | ||
<ToggleMounted | ||
queryKeys={[['story-initialData-success'], ['story-initialData-error']]} | ||
> | ||
<SideBySide> | ||
<DataContainer | ||
options={{ | ||
queryKey: ['story-initialData-success'], | ||
queryFn: fetchData, | ||
enabled: false, | ||
initialData: {tmp: 'data'}, | ||
}} | ||
> | ||
<div>status begins as "success"</div> | ||
</DataContainer> | ||
<DataContainer | ||
options={{ | ||
queryKey: ['story-initialData-error'], | ||
queryFn: fetchThrowsError, | ||
retry: 0, | ||
enabled: false, | ||
initialData: {tmp: 'data'}, | ||
}} | ||
> | ||
<div>expected to throw an error</div> | ||
</DataContainer> | ||
</SideBySide> | ||
</ToggleMounted> | ||
); | ||
}); | ||
|
||
story('suspense: true, useErrorBoundary: false', () => { | ||
return ( | ||
<ToggleMounted queryKeys={[['story-suspense1-success'], ['story-suspense1-error']]}> | ||
<Suspense fallback={<LoadingFallback />}> | ||
<DataContainer | ||
options={{ | ||
queryKey: ['story-suspense1-success'], | ||
queryFn: fetchData, | ||
suspense: true, | ||
useErrorBoundary: false, | ||
}} | ||
/> | ||
</Suspense> | ||
<Suspense fallback={<LoadingFallback />}> | ||
<DataContainer | ||
options={{ | ||
queryKey: ['story-suspense1-error'], | ||
queryFn: fetchThrowsError, | ||
retry: 0, | ||
suspense: true, | ||
useErrorBoundary: false, | ||
}} | ||
/> | ||
</Suspense> | ||
</ToggleMounted> | ||
); | ||
}); | ||
|
||
story('suspense: true, without <Suspense>', () => { | ||
return ( | ||
<ToggleMounted queryKeys={[['story-suspense2-success'], ['story-suspense2-error']]}> | ||
<DataContainer | ||
options={{ | ||
queryKey: ['story-suspense2-success'], | ||
queryFn: fetchData, | ||
suspense: true, | ||
useErrorBoundary: false, | ||
}} | ||
/> | ||
|
||
<DataContainer | ||
options={{ | ||
queryKey: ['story-suspense2-error'], | ||
queryFn: fetchThrowsError, | ||
retry: 0, | ||
suspense: true, | ||
useErrorBoundary: false, | ||
}} | ||
/> | ||
</ToggleMounted> | ||
); | ||
}); | ||
|
||
story('useErrorBoundary: true', () => { | ||
return ( | ||
<p> | ||
TODO: I was unable to get an example to work with react-query v4 and react 18.2.0 | ||
</p> | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I not sure we should encourage checking for
isFetching || isLoading
, since you'll lose the benefits of background refetches if you do soThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'll make more examples!