File tree Expand file tree Collapse file tree 3 files changed +50
-3
lines changed
Expand file tree Collapse file tree 3 files changed +50
-3
lines changed Original file line number Diff line number Diff line change @@ -24,13 +24,14 @@ export class QueryObserver<TResult, TError> {
2424 this . refetch = this . refetch . bind ( this )
2525 this . fetchMore = this . fetchMore . bind ( this )
2626
27- // Subscribe to query
27+ // Subscribe to the query
2828 this . updateQuery ( )
2929 }
3030
3131 subscribe ( listener ?: UpdateListener < TResult , TError > ) : ( ) => void {
3232 this . started = true
3333 this . updateListener = listener
34+ this . currentQuery . subscribeObserver ( this )
3435 this . optionalFetch ( )
3536 this . updateRefetchInterval ( )
3637 return this . unsubscribe . bind ( this )
@@ -204,11 +205,14 @@ export class QueryObserver<TResult, TError> {
204205 }
205206
206207 this . previousResult = this . currentResult
207- prevQuery ?. unsubscribeObserver ( this )
208208 this . currentQuery = newQuery
209- newQuery . subscribeObserver ( this )
210209 this . currentResult = this . createResult ( )
211210
211+ if ( this . started ) {
212+ prevQuery ?. unsubscribeObserver ( this )
213+ this . currentQuery . subscribeObserver ( this )
214+ }
215+
212216 return true
213217 }
214218
Original file line number Diff line number Diff line change @@ -234,6 +234,48 @@ describe('useQuery', () => {
234234 } )
235235 } )
236236
237+ // https://github.com/tannerlinsley/react-query/issues/896
238+ it ( 'should fetch data in Strict mode when refetchOnMount is false' , async ( ) => {
239+ const key = queryKey ( )
240+ const states : QueryResult < string > [ ] = [ ]
241+
242+ function Page ( ) {
243+ const state = useQuery (
244+ key ,
245+ async ( ) => {
246+ await sleep ( 10 )
247+ return 'test'
248+ } ,
249+ {
250+ refetchOnMount : false ,
251+ }
252+ )
253+ states . push ( state )
254+ return null
255+ }
256+
257+ render (
258+ < React . StrictMode >
259+ < Page />
260+ </ React . StrictMode >
261+ )
262+
263+ await waitFor ( ( ) => expect ( states . length ) . toBe ( 4 ) )
264+
265+ expect ( states [ 0 ] ) . toMatchObject ( {
266+ data : undefined ,
267+ } )
268+ expect ( states [ 1 ] ) . toMatchObject ( {
269+ data : undefined ,
270+ } )
271+ expect ( states [ 2 ] ) . toMatchObject ( {
272+ data : 'test' ,
273+ } )
274+ expect ( states [ 3 ] ) . toMatchObject ( {
275+ data : 'test' ,
276+ } )
277+ } )
278+
237279 it ( 'should share equal data structures between query results' , async ( ) => {
238280 const key = queryKey ( )
239281
Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ export function useBaseQuery<TResult, TError>(
4242 }
4343
4444 if ( config . enabled && config . suspense && ! result . isSuccess ) {
45+ observer . subscribe ( )
4546 throw observer . fetch ( ) . finally ( ( ) => {
4647 observer . unsubscribe ( true )
4748 } )
You can’t perform that action at this time.
0 commit comments