Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

370
Views
SWR: How do you use the compare option in the SWR hook?

According to the documentation of SWR: React Hooks for Data Fetching, there is an options object which can be passed in for more control of the hook.

The documentation mentions a compare option:

compare(a, b): comparison function used to detect when returned data has changed, to avoid spurious rerenders. By default, dequal is used.

Is it something like this?

export function useUser(shouldAPIBeCalled = false) {
  const { data: user, error, mutate } = useSWR(() => shouldAPIBeCalled ? '/api/user' : null, fetcher, {

  compare: (_old, _new) => {
    return dequal(_old, _new);
  }
})

Essentially I don't want the fetcher to be called if we haven't changed the original object in my case:

If the user is still...

user: null // don't make that call again

Don't make another call because it hasn't changed!

What’s happening to me is the hook keeps firing.

I only have the hook in a Layout component to show hide navigation elements if we have a user or not,

In the submit function of the login to get the mutate function to update the user,

And in the Auth component (which wraps certain pages) to check if a user is authorized.

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!