ReactJS: Warning: setState(…): Cannot update during an existing state transition

后端 未结 11 1069
一个人的身影
一个人的身影 2020-11-27 10:32

I am trying to refactor the following code from my render view:

相关标签:
11条回答
  • 2020-11-27 10:59

    I am giving a generic example for better understanding, In the following code

    render(){
        return(
          <div>
    
            <h3>Simple Counter</h3>
            <Counter
              value={this.props.counter}
              onIncrement={this.props.increment()} <------ calling the function
              onDecrement={this.props.decrement()} <-----------
              onIncrementAsync={this.props.incrementAsync()} />
          </div>
        )
      }
    

    When supplying props I am calling the function directly, this wold have a infinite loop execution and would give you that error, Remove the function call everything works normally.

    render(){
        return(
          <div>
    
            <h3>Simple Counter</h3>
            <Counter
              value={this.props.counter}
              onIncrement={this.props.increment} <------ function call removed
              onDecrement={this.props.decrement} <-----------
              onIncrementAsync={this.props.incrementAsync} />
          </div>
        )
      }
    
    0 讨论(0)
  • 2020-11-27 11:01

    That usually happens when you call

    onClick={this.handleButton()} - notice the () instead of:

    onClick={this.handleButton} - notice here we are not calling the function when we initialize it

    0 讨论(0)
  • 2020-11-27 11:04

    The solution that I use to open Popover for components is reactstrap (React Bootstrap 4 components).

        class Settings extends Component {
            constructor(props) {
                super(props);
    
                this.state = {
                  popoversOpen: [] // array open popovers
                }
            }
    
            // toggle my popovers
            togglePopoverHelp = (selected) => (e) => {
                const index = this.state.popoversOpen.indexOf(selected);
                if (index < 0) {
                  this.state.popoversOpen.push(selected);
                } else {
                  this.state.popoversOpen.splice(index, 1);
                }
                this.setState({ popoversOpen: [...this.state.popoversOpen] });
            }
    
            render() {
                <div id="settings">
                    <button id="PopoverTimer" onClick={this.togglePopoverHelp(1)} className="btn btn-outline-danger" type="button">?</button>
                    <Popover placement="left" isOpen={this.state.popoversOpen.includes(1)} target="PopoverTimer" toggle={this.togglePopoverHelp(1)}>
                      <PopoverHeader>Header popover</PopoverHeader>
                      <PopoverBody>Description popover</PopoverBody>
                    </Popover>
    
                    <button id="popoverRefresh" onClick={this.togglePopoverHelp(2)} className="btn btn-outline-danger" type="button">?</button>
                    <Popover placement="left" isOpen={this.state.popoversOpen.includes(2)} target="popoverRefresh" toggle={this.togglePopoverHelp(2)}>
                      <PopoverHeader>Header popover 2</PopoverHeader>
                      <PopoverBody>Description popover2</PopoverBody>
                    </Popover>
                </div>
            }
        }
    
    0 讨论(0)
  • 2020-11-27 11:06

    You can't modify the state of your component on the render method, it'll re-render and you'll get into an infinite loop. This solution worked for me: http://txbmcode.com/index.php/2020/06/21/reactjs-error-cannot-update-during-an-existing-state-transition-such-as-within-render-render-methods-should-be-a-pure-function-of-props-and-state/

    0 讨论(0)
  • 2020-11-27 11:08

    From react docs Passing arguments to event handlers

    <button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
    <button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>
    
    0 讨论(0)
提交回复
热议问题