setQueryData not re-rendering the React Component

See original GitHub issue

Issue:

I’m running setQueryData to update query data while also updating the internal state of the component. This causes the component to not re-render after the change in query data

const updater = (nextData) => {
    /**
     * Appending new task to the top of the array
     * but react component is not re-rendering...
     */
    queryClient.setQueryData("todos", (oldData) => {
      return [...nextData, ...oldData];
    });
};

Inside React component

<button onClick={() => {
          updater([
            {
              id: 0,
              title: newTaskText
            }
          ]);
          setNewTaskText("new task");
        }}
      >
        Add
</button>

To Reproduce

Refer the codesandbox

  • Clicking on Add Task will add the new task to query data
  • But you need to click add task twice to see it re-render in the UI

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10

github_iconTop GitHub Comments

6reactions
TkDodocommented, Jan 6, 2021

@utkarshgupta137 your example has nothing to do with the issue i believe. Please don’t mutate existing cache values but return a new object / array instead. You can also use immer if you don’t like the spread syntax.

1reaction
boschnicommented, Dec 30, 2020

After executing the onClick function the component is re-rendered and useQuery is returning the new data ([]). But it seems like React is discarding this render because the state did not change. After that a notification from the cache comes in, telling useQuery to re-render because of the new data, but because useQuery thinks it already rendered this data, it does not trigger an additional re-render. This was an optimization to prevent unnecessary renders, but if certain renders are discarded, then we cannot rely on it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

setQueryData not re-rendering the React Component #1535
The issue seems to be that useState doesn't trigger a re-render if the value in the state is the same as the current...
Read more >
React-query setQueryData not re-rendering component
I'm using React-query as a server state management library and I'm trying to get my UI state synchronized with my server state when...
Read more >
queryclient.setQueryData-no-re-render - CodeSandbox
Activating extension 'vscode.typescript-language-features' failed: Could not find bundled tsserver.js.
Read more >
When does React re-render components? - Felix Gerschau
Force a React component to rerender. Using React's forceUpdate function; Force an update in React hooks. How to optimize re-renders.
Read more >
What I learned from React-Query, and why I won't use it again
selectedSectionData is used on various components in our app, and also sent to an Iframe window. We think that such a process should...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found