Replies: 2 comments 1 reply
|
Creating components inside components (nested components) will cause React to throw away the state of those nested components on each re-render of their parent. You are creating the You can use some memoisation ( For such cases, there is a react/no-unstable-nested-components rule for ESLint, which provides a more detailed description with reasons and examples. |
|
There are two interconnected reasons this bug is occurring: what triggers the window-focus re-render, and why that re-render destroys your active input values. 1. What triggers the re-render on window focus?If your page fetches data using SWR, TanStack Query (React Query), or a Next.js client-side router listener, they enable window-focus revalidation by default:
Whenever you switch tabs or click back into the browser window, a background fetch is dispatched. When the server response resolves, the parent page component re-renders with fresh data. 2. Why does that re-render wipe out the input values?Normally, a parent re-render does not reset the local Cause A:
|
Uh oh!
There was an error while loading. Please reload this page.
I created my own data-table.tsx component that uses shadcn's and tanstack's table components. The table works fine, up until I discovered a very strange and infuriating bug, which is, when I write a value inside of a cell component and i refocus the window, the whole component re-renders for no known reason. I have tried to replace the custom input with a simple input, without any other events besides onChange, but no luck.
I have no idea what is causing this re-render but any help is greatly appreciated.
Below are the components at fault:
-- data-table.tsx
Columns.tsx
ProductInput.tsx
All reactions