Skip to content

Fix useNumberFieldStately dep on aria #2918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 4, 2022

Conversation

snowystinger
Copy link
Member

@snowystinger snowystinger commented Mar 3, 2022

Closes
Make use of https://reactjs.org/docs/hooks-faq.html#how-do-i-implement-getderivedstatefromprops

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

Sanity check NumberField stories

🧢 Your Project:

@adobe-bot
Copy link

Build successful! 🎉

ktabors
ktabors previously approved these changes Mar 4, 2022
Copy link
Member

@ktabors ktabors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change worked. I only tested NumberField

useLayoutEffect(() => {
let [prevValue, setPrevValue] = useState(numberValue);
let [prevLocale, setPrevLocale] = useState(locale);
let [prevFormatOptions, setPrevFormatOptions] = useState(formatOptions);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be refs, not state. Setting them should not trigger a re-render. You could potentially also combine them into a single ref, that's an object containing them all?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if they haven't changed, then setting then won't cause a re-render
we only get in here if one of them has changed, and we already call setState on the main one we're interested in
the setStates will get aggregated and result in only one new render

Note, this is the way that the react docs say to implement it as well https://reactjs.org/docs/hooks-faq.html#how-do-i-implement-getderivedstatefromprops

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but I still don't get why it needs to be a state and not a ref? ref seems better for all things that don't need to cause a render.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i'm not sure why they implement it with state, I've moved it over anyways. they should have the same behavior

@adobe-bot
Copy link

Build successful! 🎉

@adobe-bot
Copy link

Build successful! 🎉

@adobe-bot
Copy link

Build successful! 🎉

@adobe-bot
Copy link

Build successful! 🎉

@adobe-bot
Copy link

Build successful! 🎉

@snowystinger snowystinger merged commit 8cef9a5 into main Mar 4, 2022
@snowystinger snowystinger deleted the fix-numberfield-state-dependency branch March 4, 2022 21:23
@snowystinger snowystinger mentioned this pull request Mar 10, 2022
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants