1515 */
1616
1717import { useEffect , useRef } from 'react' ;
18+ import { setAction } from 'fetchye-core' ;
1819import { runAsync } from './runAsync' ;
1920import { computeKey } from './computeKey' ;
2021import {
2122 isLoading ,
2223} from './queryHelpers' ;
2324import { useFetchyeContext } from './useFetchyeContext' ;
2425
25- const passInitialData = ( value , initialValue , numOfRenders ) => ( numOfRenders === 1
26- ? value || initialValue
27- : value ) ;
26+ const passInitialData = ( {
27+ value,
28+ initialValue,
29+ numOfRenders,
30+ forceInitialFetch,
31+ } ) => {
32+ if ( numOfRenders === 1 ) {
33+ if ( initialValue ) {
34+ return initialValue ;
35+ }
36+ if ( forceInitialFetch === true ) {
37+ return undefined ;
38+ }
39+ }
40+ return value ;
41+ } ;
2842
2943const useFetchye = (
3044 key ,
@@ -37,6 +51,7 @@ const useFetchye = (
3751 const selectedFetcher = typeof fetcher === 'function' ? fetcher : defaultFetcher ;
3852 const computedKey = computeKey ( key , options ) ;
3953 const selectorState = useFetchyeSelector ( computedKey . hash ) ;
54+ const forceInitialFetch = useRef ( options ?. forceInitialFetch || false ) ;
4055 // create a render version manager using refs
4156 const numOfRenders = useRef ( 0 ) ;
4257 numOfRenders . current += 1 ;
@@ -50,18 +65,25 @@ const useFetchye = (
5065 return ;
5166 }
5267 const { loading, data, error } = selectorState . current ;
53- // If first render and options.forceInitialFetch is true we want to fetch from server
54- // on first render.
55- if (
56- ( ! loading && ! data && ! error )
57- || ( numOfRenders . current === 1 && options . forceInitialFetch === true )
58- ) {
68+
69+ if ( data && forceInitialFetch . current ) {
70+ // This is so it clears the cache before the forceFetch so we don't have isLoading true
71+ // and data also defined from the cached value.
72+ dispatch ( setAction ( { hash : computedKey . hash , value : undefined } ) ) ;
5973 runAsync ( {
6074 dispatch, computedKey, fetcher : selectedFetcher , fetchClient, options,
6175 } ) ;
76+ forceInitialFetch . current = false ;
77+ return ;
6278 }
63- } ) ;
6479
80+ if ( ! loading && ! data && ! error ) {
81+ runAsync ( {
82+ dispatch, computedKey, fetcher : selectedFetcher , fetchClient, options,
83+ } ) ;
84+ forceInitialFetch . current = false ;
85+ }
86+ } ) ;
6587 return {
6688 isLoading : isLoading ( {
6789 loading : selectorState . current . loading ,
@@ -70,14 +92,20 @@ const useFetchye = (
7092 options,
7193 } ) ,
7294 error : passInitialData (
73- selectorState . current . error ,
74- options . initialData ?. error ,
75- numOfRenders . current
95+ {
96+ value : selectorState . current . error ,
97+ initialValue : options . initialData ?. error ,
98+ numOfRenders : numOfRenders . current ,
99+ forceInitialFetch : options . forceInitialFetch ,
100+ }
76101 ) ,
77102 data : passInitialData (
78- selectorState . current . data ,
79- options . initialData ?. data ,
80- numOfRenders . current
103+ {
104+ value : selectorState . current . data ,
105+ initialValue : options . initialData ?. data ,
106+ numOfRenders : numOfRenders . current ,
107+ forceInitialFetch : options . forceInitialFetch ,
108+ }
81109 ) ,
82110 run ( ) {
83111 return runAsync ( {
0 commit comments