react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

Calculated TableView columns widths are sometimes inaccurate due to state `setTableWidth`

Open LFDanLu opened this issue 3 years ago โ€ข 2 comments

๐Ÿ› 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

LFDanLu avatar Sep 16 '22 00:09 LFDanLu

Rough code flow:

  1. setTableWidth is computed every render. It makes use of columns obtained from state(i.e. state.collection from useTableState) which isn't a problem so long as you use the updated setTableWidth when calling it from other hooks/code. (Side note: probably should make this a useCallback)
  2. TableView uses setTableWidth in https://github.com/adobe/react-spectrum/blob/e514827f49696a9c20fb819bd9610651f7017039/packages/%40react-spectrum/table/src/TableView.tsx#L400-L411. Since this makes use of useCallback, it doesn't recompute onVisibleRectChange when setTableWidth changes, meaning we may not be computing with the latest columns received via props

LFDanLu avatar Sep 16 '22 01:09 LFDanLu

setTableWidth is a new function every render, it should probably be memo'd as well, or onVisibleRectChange will also update every render.

snowystinger avatar Sep 16 '22 22:09 snowystinger

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?

snowystinger avatar Dec 16 '22 21:12 snowystinger

Nah, didn't have a reproduction of the above error but I'll ping the team that did to try it out

LFDanLu avatar Dec 16 '22 21:12 LFDanLu

Closing, inactivity and should be fixed

snowystinger avatar Feb 14 '23 21:02 snowystinger