Skip to content
Discussion options

You must be logged in to vote

With same question, I saw the source code, realizing SWR is really amazing!

The answer is use-swr.ts and utils/state.ts.

useStateWithDeps in utils/state.ts export [stateRef, stateDependenciesRef, setState]. When we setState, it will not rerender directly. However, it checks the stateDependenciesRef, and only rerenders when accessed states change.

The most magic thing is how to detect accessed states (btw, before seeing the code, I thought only babel could make it):
Just see the code: the object that useSWR return is:

  return {
    mutate: boundMutate,
    get data() {
      stateDependencies.data = true
      return data
    },
    get error() {
      stateDependencies.error = true
      r…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@pke
Comment options

@icyJoseph
Comment options

@pke
Comment options

@icyJoseph
Comment options

Answer selected by pke
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants