In ReactJS, why does `setState` behave differently when called synchronously?

后端 未结 6 726
南笙
南笙 2020-11-29 21:19

I am trying to understand the underlying cause for some somewhat \"magical\" behavior I am seeing that I cannot fully explain, and which is not apparent from reading the Rea

6条回答
  •  半阙折子戏
    2020-11-29 21:55

    Using defaultValue rather than value resolved the issue for me. I'm unsure if this is the best solution though, for example:

    From:

    return React.DOM.input({value: valueToSet,
        onChange: this.changeHandler});
    

    To:

    return React.DOM.input({defaultValue: valueToSet,
        onChange: this.changeHandler});
    

    JS Bin Example

    http://jsbin.com/xusefuyucu/edit?js,output

提交回复
热议问题