React - change input defaultValue by passing props

后端 未结 7 2000
星月不相逢
星月不相逢 2020-12-24 13:02

Consider this example:

var Field = React.createClass({
    render: function () {
        // never renders new value...
        return (
            

        
7条回答
  •  醉话见心
    2020-12-24 13:34

    I'm fairly certain this has to do with Controlled vs. Uncontrolled inputs.

    If I understand correctly, since your is Uncontrolled (doesn't define a value attribute), then the value will always resolve to the value that it is initialized with. In this case Hello!.

    In order to overcome this issue, you can add a value attribute and set it during the onChange:

    var Field = React.createClass({
          render: function () {
              // never renders new value...
              return (
                  
    ); } });

    Here is a plunker showing the change.

提交回复
热议问题