From 369d8260668435ed38570f9221a43307c2868501 Mon Sep 17 00:00:00 2001 From: Kier Borromeo Date: Mon, 15 Jul 2024 22:15:02 +0800 Subject: [PATCH 1/2] docs: access latest data for useMutationState (#7738) --- .../react/reference/useMutationState.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/framework/react/reference/useMutationState.md b/docs/framework/react/reference/useMutationState.md index a86e62eca5..ce01741666 100644 --- a/docs/framework/react/reference/useMutationState.md +++ b/docs/framework/react/reference/useMutationState.md @@ -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** From 46359542f00203c0fdf8931073a2d8a49ab71f37 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Mon, 15 Jul 2024 17:20:41 +0200 Subject: [PATCH 2/2] chore: prettier --- docs/framework/react/reference/useMutationState.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/framework/react/reference/useMutationState.md b/docs/framework/react/reference/useMutationState.md index ce01741666..cae80e2380 100644 --- a/docs/framework/react/reference/useMutationState.md +++ b/docs/framework/react/reference/useMutationState.md @@ -36,7 +36,6 @@ const data = useMutationState({ filters: { mutationKey }, select: (mutation) => mutation.state.data, }) - ``` **Example 3: Access the latest mutation data via the `mutationKey`**