Skip to content
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

docs: clarify usage of useSWRMutation with optimisticData #584

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

joeltio
Copy link

@joeltio joeltio commented Jul 18, 2024

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 shuding and huozhi as code owners July 18, 2024 05:01
Copy link

vercel bot commented Jul 18, 2024

@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