Skip to content

SWR Optimization #649

Answered by shuding
ghost asked this question in General
Discussion options

You must be logged in to vote

Won't the refetch trigger a rerender?

SWR uses 2 techniques to avoid unnecessary re-renders. In conclusion, the case you mentioned will not happen.

Deep Comparison

Every time a "fetch" (revalidate) is triggered and when the data arrives, SWR uses deep comparison to check if the data has changed. If nothing changes, SWR will not update the state and no re-render will happen.

Currently, it uses this lib to check the data equality.

Dependency Detection

A normal use case of SWR is that you have some states being used by the component:

const { data, isValidating } = useSWR(...)

The component will be rendered 3 times, caused by this 3 state changes:

data=undefined, isValidating=falsedata=u…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by shuding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant