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.

Infinite loop (Maximum call stack size exceeded) when passing redux form as a prop

See original GitHub issue

EDIT:

Apparently it only happens when the component where the form is being rendered is connected to the redux store and one of the properties is being enhanced on the mapStateToProps function, for example:

const mapStateToProps = state => ({
  // This causes the error
  clicks: { enhanced: true, count: state.clicks.count },
  // This works
  // clicks: state.clicks
});

Passing a redux form component as a prop to another component is causing an infinite loop. This only happens if you pass it using an anonymous function (functional component).

Here is an example:

const NewMessageForm = reduxForm({
  form: 'newMessageForm'
})(({ handleSubmit }) => (
  <Form onSubmit={handleSubmit}>
    <Field
      name="text"
      component="input"
      placeholder="Add your message here"
    />
  </Form>
));

function Wrapper({ newMessageForm: NewMessageForm }) {
  return (
    <div className="wrapper">
      <NewMessageForm />
     </div>
  );
}

function handleSubmit(values) {
  console.log('submit', values);
}

function Counter () {
  return (
    <div>
      <h1>Hello World!</h1>
      // Here is where the problem happens
      <Wrapper newMessageForm={() => <NewMessageForm onSubmit={handleSubmit} /> }/>     
    </div>
  );
}

This error doesn’t happen if the component is passed like this:

<Wrapper newMessageForm={NewMessageForm}/> 

or even like this:

constructor(props) {
  super(props)
  this.NewMessageForm = () =>  <NewMessageForm onSubmit={handleSubmit} />
}
render() {
   .....
   <Wrapper newMessageForm={this.NewMessageForm}/> 
   .....
}

I have an example here

What’s your environment?

Tested on chrome using redux-form@6.5.0

Other informations

Stack Trace:

Uncaught RangeError: Maximum call stack size exceeded
    at traverseAllChildrenImpl (traverseAllChildren.js:65)
    at traverseAllChildrenImpl (traverseAllChildren.js:93)
    at traverseAllChildren (traverseAllChildren.js:172)
    at flattenChildren (flattenChildren.js:66)
    at ReactDOMComponent._reconcilerUpdateChildren (ReactMultiChild.js:204)
    at ReactDOMComponent._updateChildren (ReactMultiChild.js:312)
    at ReactDOMComponent.updateChildren (ReactMultiChild.js:299)
    at ReactDOMComponent._updateDOMChildren (ReactDOMComponent.js:936)
    at ReactDOMComponent.updateComponent (ReactDOMComponent.js:754)
    at ReactDOMComponent.receiveComponent (ReactDOMComponent.js:716)

I’ve tried to reproduce this bug in an unit test but, as for today, I wasn’t able to.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:56
  • Comments:40 (4 by maintainers)

github_iconTop GitHub Comments

43reactions
rostislav-simonikcommented, May 9, 2018

I’ve found issue. validate on Field element must point to the same reference otherwise given Field is reregistered. Following code caused given issue with construction that I’ve mention in comment above .

<Field
  validate = { [
     (value) => {},
  ] }
</Field>
32reactions
leeroybruncommented, Apr 26, 2017

I had the same issue. My form was re-rendering infinitely when the user added an object to a FieldArray.

The nextProps/this.props and nextState/this.state were all the sames in shouldComponentUpdate, but it was still re-rendering.

I was able to fix this by overriding shouldComponentUpdate and check if nextProps/this.props and nextState/this.state were not strictly equal.

class FormParent extends React.PureComponent {
  shouldComponentUpdate(nextProps, nextState) {
    return JSON.stringify(this.props) !== JSON.stringify(nextProps) || JSON.stringify(this.state) !== JSON.stringify(nextState);
  }

  render() {
    return (
      <TheForm onSubmit={data => onSaveAction(data)} />
    )
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - Infinite loop (Maximum call stack size exceeded ...
Infinite loop (Maximum call stack size exceeded) when passing redux form as a prop.
Read more >
React-Redux Maximum call stack size exceeded when adding ...
I am creating a simple game react app and when I try to add a player to my players list it seems to...
Read more >
Error RangeError: Maximum call stack size exceeded
Data is passed to control using props. We get " RangeError: Maximum call stack size exceeded" if we do following steps.
Read more >
React boilerplate : Maximum call stack size exceeded-Reactjs
Coding example for the question React boilerplate : Maximum call stack size exceeded-Reactjs.
Read more >
RangeError: Maximum call stack size exceeded - Educative.io
The most common source for this error is infinite recursion. You must have a recursive function in your code whose base case is...
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