question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useNumberField: old value flash after blur in controlled mode

See original GitHub issue

🐛 Bug Report

When consuming useNumberField in controlled mode the previously set value will be visible briefly after blurring the input.

🤔 Expected Behavior

The input should not display the “old” value after blurring.

😯 Current Behavior

After entering a new value in controlled mode and blurring the input the previous value will flash up for a second before the new value from props is applied again.

💁 Possible Solution

The visual bug seems to be related to this useEffect in useNumberFieldState:

https://github.com/adobe/react-spectrum/blob/main/packages/@react-stately/numberfield/src/useNumberFieldState.ts#L103

  // Update the input value when the number value or format options change. This is done
  // in a useEffect so that the controlled behavior is correct and we only update the
  // textfield after prop changes.
  useEffect(() => {
    setInputValue(format(numberValue));
  }, [numberValue, locale, formatOptions]);

Switching the effect to a useLayoutEffect removes the visual glitch as the updated value is set before the browser repaints.

However, the input will still have one render cycle with the old value again (can be seen in code sample)

💻 Code Sample

Codepen showing the issue: https://codesandbox.io/s/amazing-oskar-zb73h8?file=/src/App.js (based on controlled example in docs, glitch is also visible there)

To reproduce:

  • Enter a new value in the input
  • Click outside the input to blur
  • Old value will flash up briefly
  • console.log shows the single render cycle with the old value after blurring

🌍 Your Environment

Software Version(s)
react-aria/numberfield 3.1.3
Browser Chrome 98.0.4758.82
Operating System Win 10

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
snowystingercommented, Nov 3, 2022

@peteragarclover I’ve made a comment so we know there is a second reported issue here. Thanks for reporting.

1reaction
devrntcommented, May 4, 2022

Yes indeed, thanks! We came across a second issue that was “caused” by StrictMode (and React 18 related). Anyway thanks for these awesome libraries (react-aria and react-stately), it has been a pleasure using them!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I check if a value is changed on blur event?
What I would do is, add a function to the focus event that saves the current value into a variable attached to the...
Read more >
useNumberField – React Aria
Number fields consist of an input element that shows the current value and allows the user to type a new value, optional stepper...
Read more >
react-md: Versions - Openbase
@react-md/layout: Update snapshots after updating tree component (ec84800) ... added a useNumberField hook to control number field values (c705f2c) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found