vercel / swr-site

The official website for SWR.
https://swr.vercel.app
Apache License 2.0
481 stars 351 forks source link

docs: clarify usage of useSWRMutation with optimisticData #584

Open joeltio opened 1 month ago

joeltio commented 1 month ago

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 https://github.com/vercel/swr/issues/2944

Description

vercel[bot] commented 1 month ago

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

A member of the Team first needs to authorize it.