[Bug]: SelectableTile onChange method is presented with stale 'selected' value #18478
Open
2 tasks done
Labels
component: tile
package: @carbon/react
@carbon/react
role: dev 🤖
severity: 3
https://ibm.biz/carbon-severity
type: bug 🐛
Package
@carbon/react
Browser
Chrome
Package version
1.74.0
React version
v18.3.1
Description
When the SelectedTile component is in the 'unselected' state and we trigger a change (via click or keyboard), the onChange method is presented with a
selected
value that is stale. In this case, it is presented withfalse.
The UI redraws correctly and properly updates all the data as far as I can tell.The defect stems from calling
onChange
after callingsetIsSelected
and assuming thatsetIsSelected
will impact the value ofisSelected
during this call. It will not, it will only drive a re-render.https://react.dev/learn/state-as-a-snapshot#state-over-time
A suggested fix would be to use the same value that is passed to
setIsSelected
to also be passed toonChange
Reproduction/example
https://stackblitz.com/edit/github-grcad4ps?file=src%2FApp.jsx
Steps to reproduce
Access the example provided and click the selectable tile in the preview area. Observe the incorrect value presented to the method.
Suggested Severity
Severity 3 = User can complete task, and/or has a workaround within the user experience of a given component.
Application/PAL
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: