setState() inside of componentDidUpdate()

后端 未结 6 2052
鱼传尺愫
鱼传尺愫 2020-11-28 03:23

I\'m writing a script which moves dropdown below or above input depending on height of dropdown and position of the input on the screen. Also I want to set modifier to dropd

6条回答
  •  醉酒成梦
    2020-11-28 04:04

    If you use setState inside componentDidUpdate it updates the component, resulting in a call to componentDidUpdate which subsequently calls setState again resulting in the infinite loop. You should conditionally call setState and ensure that the condition violating the call occurs eventually e.g:

    componentDidUpdate: function() {
        if (condition) {
            this.setState({..})
        } else {
            //do something else
        }
    }
    

    In case you are only updating the component by sending props to it(it is not being updated by setState, except for the case inside componentDidUpdate), you can call setState inside componentWillReceiveProps instead of componentDidUpdate.

提交回复
热议问题