@@ -29,6 +29,7 @@ export default function useFetch({
2929 condition = true
3030} ) {
3131 const firstRun = useRef ( true ) ;
32+ const firstClientRun = useRef ( true ) ;
3233 const uid = `fetch_${ useUID ( ) } ` ;
3334 const [ requestKey ] = useState ( options . requestKey ?? `fetch_${ useUID ( ) } ` ) ;
3435 const count = useRef ( 1 ) ;
@@ -110,19 +111,6 @@ export default function useFetch({
110111 ]
111112 : dependencies ;
112113
113- /* eslint-disable react-hooks/exhaustive-deps */
114- useEffect ( ( ) => {
115- triggerFetchData ( ) . then (
116- ( ) => {
117- if ( isFunction ( afterFetch ) ) afterFetch ( ) ;
118- } ,
119- ( ) => {
120- // do nothing
121- }
122- ) ;
123- } , [ triggerFetchData , ...refetchDependencies ] ) ;
124- /* eslint-enable react-hooks/exhaustive-deps */
125-
126114 const data = useSelector ( state =>
127115 entityUtils . select ( requestKey , state . entityStore )
128116 ) ;
@@ -137,6 +125,28 @@ export default function useFetch({
137125
138126 const response = getState ( ) ?. entityStore . responses [ requestKey ] ;
139127
128+ /* eslint-disable react-hooks/exhaustive-deps */
129+ useEffect ( ( ) => {
130+ // if first run on client, only fetch if data isn't already in redux store
131+ if ( firstClientRun . current ) {
132+ firstClientRun . current = false ;
133+ if ( loaded ) {
134+ if ( isFunction ( afterFetch ) ) afterFetch ( ) ;
135+ return ;
136+ }
137+ }
138+
139+ triggerFetchData ( ) . then (
140+ ( ) => {
141+ if ( isFunction ( afterFetch ) ) afterFetch ( ) ;
142+ } ,
143+ ( ) => {
144+ // do nothing
145+ }
146+ ) ;
147+ } , [ triggerFetchData , ...refetchDependencies ] ) ;
148+ /* eslint-enable react-hooks/exhaustive-deps */
149+
140150 firstRun . current = false ;
141151 return { data, meta, loaded, uid, response, refresh : triggerFetchData } ;
142152}
0 commit comments