setQueryData not re-rendering the React Component
See original GitHub issueIssue:
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:
- Created 3 years ago
- Comments:10
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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
immerif you don’t like the spread syntax.After executing the
onClickfunction the component is re-rendered anduseQueryis 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, tellinguseQueryto re-render because of the new data, but becauseuseQuerythinks 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.