1
- import { useContext , useRef , useState , useEffect , useCallback } from 'react'
1
+ import {
2
+ useContext ,
3
+ useRef ,
4
+ useState ,
5
+ useMemo ,
6
+ useEffect ,
7
+ useCallback
8
+ } from 'react'
2
9
3
10
import defaultConfig , { cache } from './config'
4
11
import SWRConfigContext from './swr-config-context'
@@ -184,6 +191,8 @@ function useSWRInfinite<Data = any, Error = any>(
184
191
dataRef . current = swr . data
185
192
} , [ swr . data ] )
186
193
194
+ const boundMutate = useMemo ( ( ) => swr . mutate , [ swr ] )
195
+
187
196
const mutate = useCallback (
188
197
( data , shouldRevalidate = true ) => {
189
198
if ( shouldRevalidate && typeof data !== 'undefined' ) {
@@ -195,9 +204,9 @@ function useSWRInfinite<Data = any, Error = any>(
195
204
cache . set ( contextCacheKey , { force : true } )
196
205
}
197
206
198
- return swr . mutate ( data , shouldRevalidate )
207
+ return boundMutate ( data , shouldRevalidate )
199
208
} ,
200
- [ swr . mutate , contextCacheKey ]
209
+ [ boundMutate , contextCacheKey ]
201
210
)
202
211
203
212
// extend the SWR API
@@ -217,23 +226,11 @@ function useSWRInfinite<Data = any, Error = any>(
217
226
)
218
227
219
228
// Use getter functions to avoid unnecessary re-renders caused by triggering all the getters of the returned swr object
220
- return {
221
- get error ( ) {
222
- return swr . error
223
- } ,
224
- get data ( ) {
225
- return swr . data
226
- } ,
227
- get revalidate ( ) {
228
- return swr . revalidate
229
- } ,
230
- get isValidating ( ) {
231
- return swr . isValidating
232
- } ,
229
+ return Object . assign ( swr , {
233
230
mutate,
234
231
size,
235
232
setSize
236
- } as SWRInfiniteResponseInterface < Data , Error >
233
+ } ) as SWRInfiniteResponseInterface < Data , Error >
237
234
}
238
235
239
236
export {
0 commit comments