swr icon indicating copy to clipboard operation
swr copied to clipboard

sometimes it automatically request again ,but the key has not changed

Open vaynevayne opened this issue 1 year ago • 1 comments

I have a useUser hook ,used it in father son componnet ,it can automatic request again I don't know why ,i just want it request once

  <SWRConfig value={{ use: [withToken], shouldRetryOnError: false, revalidateOnFocus: false }}>
            <Provider {...store}>
              <RouterProvider router={router} fallbackElement={<Progress />} />
            </Provider>
          </SWRConfig>


function withToken(useSWRNext) {
  return (key, fetcher, config) => {

    const extendedFetcher = (...args) => {
      return fetcher(...args)
    }

    const token = useUserStore.getState().userInfo.token
    const schoolId = useUserStore.getState().userInfo.schoolId

    if (Array.isArray(key)) {
      key.push(token, schoolId)
    } else if (key === null) {
   
    } else {
      throw new Error('')
    }

    return useSWRNext(key, extendedFetcher, config)
  }
}

vaynevayne avatar Aug 08 '24 03:08 vaynevayne

This example needs more clarity. Would you mind creating a demo on CodeSandbox?

samuel871211 avatar Jan 26 '25 08:01 samuel871211