对`input`的React`value`属性不应该为null

时间:2018-08-01 09:29:20

标签: reactjs input

我是新来的反应者,我在组件中得到了这个input

<input
        type="text"
        placeholder="..."
        className="form-control"
        value={this.state.value}
        disabled={this.props.edit}
        readOnly={this.props.readOnly}
        onChange={(e) => this.state.value = e}
    />

现在,如果this.state.value为空,则会收到此警告:

Warning: `value` prop on `input` should not be null. Consider using an empty string to clear the component or `undefined` for uncontrolled components.

但是如果我将输入更改为:

<input
        type="text"
        placeholder="..."
        className="form-control"
        value={this.state.value || ""}
        disabled={this.props.edit}
        readOnly={this.props.readOnly}
        onChange={(e) => this.state.value = e}
    />

我无法对其进行编辑。

1 个答案:

答案 0 :(得分:2)

应该像-

onChange={(e) => this.setState({value:e}) }