Skip to content

mutate(undefined) doesn't work #1481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hazae41 opened this issue Sep 20, 2021 · 2 comments · Fixed by #1515
Closed

mutate(undefined) doesn't work #1481

hazae41 opened this issue Sep 20, 2021 · 2 comments · Fixed by #1515
Labels
bug Something isn't working

Comments

@hazae41
Copy link

hazae41 commented Sep 20, 2021

Bug report

const [fallback, setFallback] = useState<Data>(...)
const res = useSWR<Data>(..., { fallbackData: fallback })

console.log("fallback", fallback)
console.log("data", res.data)

function unset(){
  setFallback(undefined)
  res.mutate(undefined, false)
}

With this code, I expect res.data to be undefined on next render when calling unset()
However, it is still the same value even though the fallback has been set to undefined

before unset:

fallback {...}
data {...}

after unset:

fallback undefined
data {...}

Additional Context

[email protected]

@shuding
Copy link
Member

shuding commented Sep 21, 2021

Currently in SWR, undefined isn't a valid data type. It's marked as an initial value which stands for "data is missing", but you can't mutate a resource to be undefined. This is something SWR should fix (e.g. mutating a resource as deleted), while I'd suggest using null or an extra layer like { data: undefined } for now.

Note: this is unrelated to fallbackData.

@shuding shuding changed the title mutate(undefined) doesn't work with state-based fallback mutate(undefined) doesn't work Sep 21, 2021
@shuding shuding added the bug Something isn't working label Sep 21, 2021
@shuding
Copy link
Member

shuding commented Oct 1, 2021

Hey! I opened #1515 which should fix this issue.

I've noticed that mutate(undefined) works correctly with the latest version — gonna close this issue, but feel free to reopen if you can provide a reproduction!

@shuding shuding closed this as completed Oct 1, 2021
@shuding shuding reopened this Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants