Skip to content

Commit

Permalink
Merge branch 'main' into vitest/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
TkDodo authored Jul 15, 2024
2 parents 155a42f + 4635954 commit 8dc7dc7
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docs/framework/react/reference/useMutationState.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ const data = useMutationState({
})
```

**Example 3: Access the latest mutation data via the `mutationKey`**
Each invocation of `mutate` adds a new entry to the mutation cache for `gcTime` milliseconds.

To access the latest invocation, you can check for the last item that `useMutationState` returns.

```tsx
import { useMutation, useMutationState } from '@tanstack/react-query'

const mutationKey = ['posts']

// Some mutation that we want to get the state for
const mutation = useMutation({
mutationKey,
mutationFn: (newPost) => {
return axios.post('/posts', newPost)
},
})

const data = useMutationState({
// this mutation key needs to match the mutation key of the given mutation (see above)
filters: { mutationKey },
select: (mutation) => mutation.state.data,
})

// Latest mutation data
const latest = data[data.length - 1]
```

**Options**

- `options`
Expand Down

0 comments on commit 8dc7dc7

Please sign in to comment.