Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot update during an existing state transition" error in React

I'm trying to do Step 15 of this ReactJS tutorial: React.js Introduction For People Who Know Just Enough jQuery To Get By

The author recommends the following:

overflowAlert: function() {
  if (this.remainingCharacters() < 0) {
    return (
      <div className="alert alert-warning">
        <strong>Oops! Too Long:</strong>
      </div>
    );
  } else {
    return "";
  }
},

render() {
  ...

  { this.overflowAlert() }

  ...
}

I tried doing the following (which looks alright to me):

// initialized "warnText" inside "getInitialState"


overflowAlert: function() {
  if (this.remainingCharacters() < 0) {
    this.setState({ warnText: "Oops! Too Long:" });
  } else {
    this.setState({ warnText: "" });
  }
},

render() {
  ...

  { this.overflowAlert() }
  <div>{this.state.warnText}</div>

  ...
}

And I received the following error in the console in Chrome Dev Tools:

Cannot update during an existing state transition (such as within render or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to componentWillMount.

Here's a JSbin demo. Why won't my solution work and what does this error mean?

like image 679
thanks_in_advance Avatar asked Mar 12 '17 08:03

thanks_in_advance


People also ask

Why is React not updating on state change?

State updates in React are asynchronous; when an update is requested, there is no guarantee that the updates will be made immediately. The updater functions enqueue changes to the component state, but React may delay the changes, updating several components in a single pass.

How do I update my state data in React?

To update our state, we use this. setState() and pass in an object. This object will get merged with the current state.

Can we update state in componentDidMount?

You may call setState() immediately in componentDidMount() . It will trigger an extra rendering, but it will happen before the browser updates the screen. This guarantees that even though the render() will be called twice in this case, the user won't see the intermediate state.

Can we update the state in render method in React?

You can't set react state in the render function, but you can in the constructor or most of the component lifecycle functions. Show activity on this post. Never set state inside render function as it might create a side effect and it will create an infinite render cycle eventually.

What is cannot update during a state transition?

Cannot update during an existing state transition (such as within `render` or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.

Why can’t i update setState in React React?

ReactJS: Warning: setState (…): Cannot update during an existing state transition to a version where the bind is within the constructor. The reason for that is that bind in the render view will give me performance issues, especially on low end mobile phones.

Can navlink update during state transition in React state?

React: Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state I want to have active NavLink "submenu key" in React state to tell NavLink's onClick () method to check if any of NavLinks in Collapse is active and if so, don't toggle isOpen for Collapse.

How do I toggle the fixed or error component in react?

The App component renders a title and two buttons, one to show the working component with correct loading of state, and one that triggers the error. Inside App, each button triggers a state switch to toggle the Fixed or Error component. We also have a `getQSValue ()` method, which looks for a named key in the query string and returns it.


1 Answers

Your solution does not work because it doesn't make sense logically. The error you receive may be a bit vague, so let me break it down. The first line states:

Cannot update during an existing state transition (such as within render or another component's constructor).

Whenever a React Component's state is updated, the component is rerendered to the DOM. In this case, there's an error because you are attempting to call overflowAlert inside render, which calls setState. That means you are attempting to update state in render which will in then call render and overflowAlert and update state and call render again, etc. leading to an infinite loop. The error is telling you that you are trying to update state as a consequence of updating state in the first place, leading to a loop. This is why this is not allowed.

Instead, take another approach and remember what you're trying to accomplish. Are you attempting to give a warning to the user when they input text? If that's the case, set overflowAlert as an event handler of an input. That way, state will be updated when an input event happens, and the component will be rerendered.

like image 83
Andrew Li Avatar answered Sep 18 '22 17:09

Andrew Li