File tree Expand file tree Collapse file tree 2 files changed +30
-3
lines changed
Expand file tree Collapse file tree 2 files changed +30
-3
lines changed Original file line number Diff line number Diff line change @@ -59,10 +59,10 @@ export class QueryInstance<TResult, TError> {
5959 ) {
6060 this . refetchIntervalId = setInterval ( ( ) => {
6161 if (
62- this . query . instances . some ( _ => this . config . enabled ) &&
62+ this . query . instances . some ( d => d . config . enabled ) &&
6363 ( isDocumentVisible ( ) ||
6464 this . query . instances . some (
65- _ => this . config . refetchIntervalInBackground
65+ d => d . config . refetchIntervalInBackground
6666 ) )
6767 ) {
6868 this . query . fetch ( )
@@ -76,7 +76,7 @@ export class QueryInstance<TResult, TError> {
7676 try {
7777 // Perform the refetch for this query if necessary
7878 if (
79- this . query . config . enabled && // Don't auto refetch if disabled
79+ this . query . instances . some ( d => d . config . enabled ) && // Don't auto refetch if disabled
8080 ! this . query . wasSuspended && // Don't double refetch for suspense
8181 this . query . state . isStale && // Only refetch if stale
8282 ( this . query . config . refetchOnMount || this . query . instances . length === 1 )
Original file line number Diff line number Diff line change @@ -1007,4 +1007,31 @@ describe('useQuery', () => {
10071007
10081008 await waitFor ( ( ) => rendered . getByText ( '{"a":"a"}' ) )
10091009 } )
1010+
1011+ it ( 'should refetch if any query instance becomes enabled' , async ( ) => {
1012+ const queryFn = jest . fn ( ) . mockReturnValue ( 'data' )
1013+
1014+ function Disabled ( ) {
1015+ useQuery ( 'test' , queryFn , { enabled : false } )
1016+ return null
1017+ }
1018+
1019+ function Page ( ) {
1020+ const [ enabled , setEnabled ] = React . useState ( false )
1021+ const result = useQuery ( 'test' , queryFn , { enabled } )
1022+ return (
1023+ < >
1024+ < Disabled />
1025+ < div > { result . data } </ div >
1026+ < button onClick = { ( ) => setEnabled ( true ) } > enable</ button >
1027+ </ >
1028+ )
1029+ }
1030+
1031+ const rendered = render ( < Page /> )
1032+ expect ( queryFn ) . toHaveBeenCalledTimes ( 0 )
1033+ fireEvent . click ( rendered . getByText ( 'enable' ) )
1034+ await waitFor ( ( ) => rendered . getByText ( 'data' ) )
1035+ expect ( queryFn ) . toHaveBeenCalledTimes ( 1 )
1036+ } )
10101037} )
You can’t perform that action at this time.
0 commit comments