Skip to content

docs: clarify usage of useSWRMutation with optimisticData - #584

Open
joeltio wants to merge 1 commit into
vercel:mainfrom
joeltio:optimistic-swr-mutation
Open

docs: clarify usage of useSWRMutation with optimisticData#584
joeltio wants to merge 1 commit into
vercel:mainfrom
joeltio:optimistic-swr-mutation

Conversation

@joeltio

@joeltio joeltio commented Jul 18, 2024

Copy link
Copy Markdown

This PR clarifies where data comes from under the section on mutations with optimistic data:

import useSWRMutation from 'swr/mutation'

function Profile () {
  const { trigger } = useSWRMutation('/api/user', updateUserName)
  const { data } = useSWR('/api/user', fetcher) // <--- Added

  return (
    <div>
      <h1>My name is {data.name}.</h1>
      <button onClick={async () => {
        const newName = data.name.toUpperCase()

        trigger(newName, {
          optimisticData: user => ({ ...user, name: newName }),
          rollbackOnError: true
        })
      }}>Uppercase my name!</button>
    </div>
  )
}

I have also added a short note under that code snippet to bring this to the reader's attention.

See also and closes vercel/swr#2944

Description

  • Adding new page
  • Updating existing documentation
  • Other updates

@joeltio
joeltio requested review from huozhi and shuding as code owners July 18, 2024 05:01
@vercel

vercel Bot commented Jul 18, 2024

Copy link
Copy Markdown
Contributor

@joeltio is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clarification Needed on Handling data for Optimistic Updates in useSWRMutation

1 participant