Skip to content

Commit

Permalink
docs: access latest data for useMutationState (#7738)
Browse files Browse the repository at this point in the history
  • Loading branch information
srph committed Jul 15, 2024
1 parent 79e5202 commit 369d826
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/framework/react/reference/useMutationState.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@ const data = useMutationState({
filters: { mutationKey },
select: (mutation) => mutation.state.data,
})

```

**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**
Expand Down

0 comments on commit 369d826

Please sign in to comment.