From 3072279f2e43831f0b6c000120948a2fb244ca28 Mon Sep 17 00:00:00 2001 From: Daniel Kostenko <86262465+KiwiUserSFTW@users.noreply.github.com> Date: Fri, 16 Jan 2026 16:08:45 +0100 Subject: [PATCH] use setQueryData updater function for notes state update This change replaces the cache update logic with the updater function of setQueryData, which allows the existing notes state to be updated more concisely. The explanation was adjusted accordingly. "Maybe the previous version is considered more suitable for teaching purposes, this change can be treated as a light suggestion" Thank you for a great course --- src/content/6/en/part6d.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/content/6/en/part6d.md b/src/content/6/en/part6d.md index 28cc0b2243c..118b64a4053 100644 --- a/src/content/6/en/part6d.md +++ b/src/content/6/en/part6d.md @@ -379,8 +379,9 @@ const App = () => { mutationFn: createNote, // highlight-start onSuccess: (newNote) => { - const notes = queryClient.getQueryData(['notes']) - queryClient.setQueryData(['notes'], notes.concat(newNote)) + queryClient.setQueryData(['notes'], (oldNotes) => + oldNotes.concat(newNote) + ) // highlight-end } }) @@ -389,7 +390,7 @@ const App = () => { } ``` -That is, in the onSuccess callback, the queryClient object first reads the existing notes state of the query and updates it by adding a new note, which is obtained as a parameter of the callback function. The value of the parameter is the value returned by the function createNote, defined in the file requests.js as follows: +That is, in the onSuccess callback, the queryClient object updates the existing notes state of the query by using the previous state and adding the new note to it. The previous state of the query is provided as a parameter to the updater function, and the new note is obtained as a parameter of the callback function. The value of the parameter is the value returned by the function createNote, defined in the file requests.js as follows: ```js export const createNote = async (newNote) => {