react-spectrum
react-spectrum copied to clipboard
Calculated TableView columns widths are sometimes inaccurate due to state `setTableWidth`
๐ Bug Report
We've seen a case where an initially hidden column renders with a width of 0 instead of its provided minWidth. It seems to due to setTableWith here becoming stale, which causes column widths to be calculated against an outdated columns prop.
๐ค Expected Behavior
Newly added column is rendered with the proper width
๐ฏ Current Behavior
Newly added column is rendered with 0px width
๐ Possible Solution
Add setTableWidth to dep array here
๐ฆ Context
๐ป Code Sample
Sandbox example: https://codesandbox.io/s/brave-pare-ntibeu?file=/src/App.js. This example doesn't actually exhibit the broken behavior but models the setup the user was seeing.
๐ Your Environment
| Software | Version(s) |
|---|---|
| react-spectrum | 3.21.2 |
| Browser | |
| Operating System |
๐งข Your Company/Team
Rough code flow:
- setTableWidth is computed every render. It makes use of
columnsobtained fromstate(i.e. state.collection from useTableState) which isn't a problem so long as you use the updatedsetTableWidthwhen calling it from other hooks/code. (Side note: probably should make this a useCallback) - TableView uses
setTableWidthin https://github.com/adobe/react-spectrum/blob/e514827f49696a9c20fb819bd9610651f7017039/packages/%40react-spectrum/table/src/TableView.tsx#L400-L411. Since this makes use ofuseCallback, it doesn't recomputeonVisibleRectChangewhensetTableWidthchanges, meaning we may not be computing with the latest columns received via props
setTableWidth is a new function every render, it should probably be memo'd as well, or onVisibleRectChange will also update every render.
this should be fixed now by https://github.com/adobe/react-spectrum/pull/3672, @LFDanLu do you have a test case you could check against it?
Nah, didn't have a reproduction of the above error but I'll ping the team that did to try it out
Closing, inactivity and should be fixed