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.

Changing value and suffix at the same time causes Maximum update depth exceeded error

See original GitHub issue

I’m using react-number-format in combination with Formik and material-ui. I have a use case when I need to call Formiks resetForm and it updates value and a suffix at the same time. E.g. (100 m to 0 ft ).

const NumberFormatCustom = (props: *): NumberFormat => { const { inputRef, onChange, unit, ...other } = props; return ( <NumberFormat {...other} getInputRef={inputRef} onValueChange={values => { onChange({ target: { value: values.value, name: other.name, }, }); }} suffix={unit} /> ); };

NumberFormat is passed though InputProps to TextField.

InputProps={{ inputComponent: NumberFormatCustom }}

This is the console log that shows Maximum update depth exceeded error.

screen shot 2019-01-30 at 5 15 00 pm

Issue Analytics

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

github_iconTop GitHub Comments

15reactions
zhusee2commented, Jul 10, 2019

I think the problem comes from L134 in <NumberFormat>: https://github.com/s-yadav/react-number-format/blob/81ef1cfe38cd06b50e1e9753ff956abefead4da4/src/number_format.js#L134

When a component is re-rendered, the props and prevProps will not be the same instance, so this expression will always be true?

I’ve noticed a situation when a user types really fast, there is a chance that this component gets re-rendered (for any reason):

  • after it calls setState() inside updateValue() (triggered from onChange), but
  • before the setState() callback is called (so a ancestor component gets notified of new value)

And thus the component is rendered with old value when it’s controlled, and it always override its state even when the value in state is actually newer.

After that the callback is finally fired, bringing value to its ancestor component, and triggered another re-render. In my case this is where the whole thing goes into an infinite loop.

2reactions
morloycommented, Apr 14, 2019

I think I found a workaround by using

value={this.state.value || undefined}

This briefly switches the component to uncontrolled if there’s now valid input and breaks the infinite loop.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReactJS: Maximum update depth exceeded error
Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.
Read more >
Fix the "Maximum Update Depth Exceeded" Error in React
One quick solution is to move the function inside the useEffect hook. ... We can see that now incrementViews is scoped inside the...
Read more >
How to Fix Maximum Update Depth Exceeded in React and ...
In this video, I show you several ways the dreaded " Max Update Depth Exceeded " error commonly occurs in React and React...
Read more >
Documentation - Rclone
If there is a file with the same path (after the suffix has been added) in DIR, then it will be overwritten. The...
Read more >
MySQL 8.0 Reference Manual :: 5.1.8 Server System Variables
The server sets this variable whenever the default database changes. If there is no default database, the variable has the same value as ......
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