question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

concerns about passing null as fetcher

See original GitHub issue

Background

@pacocoursey 's idea for SWR as a local cache is great.

const useSharedState = (key, initial) => {
  const { data: state, mutate: setState } = useSWR(key, {
    initialData: initial
  })
  return [state, setState]
}

But after #367 this won’t work anymore because window.fetch will be used when fn is undefined https://github.com/vercel/swr/blob/10e18cc9e6e607309db1aee81ac22b58690a76c3/src/use-swr.ts#L245-L248

However passing null as fetcher will work since we only replace the fetcher to window.fetch when fetcher is undefined.

useSWR(key, null ,{ initialData: initial })

But the typescript will complain this kind of usage. #730 fix this problem by accepting null as as the fetcher function.

Problem

  1. This usage is not well documented.
  2. passing null as the fetcher function is like a hack here to disable the whole stale-while-revalidate process in swr. I am not sure it is a good api design. I feel like it somehow leaks the implementation details.
  3. Passing null as the fetcher function makes most of the options useless here.
useSWR(key, null ,{ initialData: initial, /** most of options here will not be used since the revalidation is disabled here */ })

Possible alternatives

  1. a passive options and a different options type
useSWR(key, { 
  initialData: initial, 
  passive: true 
  /** typescript should complain about set options like onSuccess 
  which will only be used at  stale-while-revalidate process  */
})
  1. a new hooks share the caches
const { data: state, mutate: setState } = useSWRPassive(key, { initialData })

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
shudingcommented, Aug 28, 2021

In SWR 1.0, there is no more default fetcher so you don’t need to pass null as fetcher anymore. Also we think the fallback API is a better way to provide “initial data”. I think we can close this issue now 😃

1reaction
lukeshumardcommented, Mar 29, 2021

I’ve spent a little bit of time playing around with this, also inspired by @pacocoursey’s blog post. His approach is a very eloquent and simple solution, but it is important to consider what using null as a fetcher means.

By passing null as the fetcher, any attempts to fetch data will return as null. This sounds obvious, but it’s important to remember that SWR can be configured to revalidate your data on focus and reconnect. If either of these events occur, the fetcher is called and your data is now null. Additionally, calling mutate could also trigger a revalidation so it’s important to be mindful of this.

Here’s my useSharedState hook based off of @shuding’s suggestion.

import useSWR from 'swr'

export default function useSharedState(key, initialData) {
  return useSWR(key, null, {
    initialData,
    revalidateOnFocus: false,
    revalidateOnReconnect: false
  })
}

In use, it looks like this.

const { data, mutate } = useSharedState('username', 'luke')

As long as the fetcher is not called again, this will result in having shared state between hooks in a single page.

Read more comments on GitHub >

github_iconTop Results From Across the Web

fetch post always pass null to the parameters - Stack Overflow
I've tested the url and parameter in native android, it works there but here it doesn't take the parameter. It always sends the...
Read more >
Null in JavaScript - GeeksforGeeks
Null is an object in JavaScript and represents primitive data types. ... If we pass null as the default parameter to any function...
Read more >
Pass null argument with method overloading of String and ...
Tricky Interview Question : Pass null argument with method ... 24K views 1 year ago Tricky Java Programming Interview Questions - By Naveen ......
Read more >
Pass NULL parameters to Stored Procedure ... - ASPSnippets
The following StoredProcedure accepts a NULL parameter @CustomerID and its value is used to fetch the Customer from the Customers table of the ......
Read more >
Passing null values in SQL query to get all values - MSDN
This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions. Learn More · SQL Server Developer Center.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found