我正在使用控制输入来创建一个带有响应的简单表单。在组件状态下,我在状态上有2个属性“ clientName”和“ license”。改变那些效果很好。但是有一个作为对象的“运输”属性。更改任何运输属性都会产生错误。例如,如果我更改“ address1”,则在handleShippingChange函数中设置状态后,就会收到错误消息:


  警告:TextField正在将文本类型的受控输入更改为
  不受控制。输入元素不应从受控切换为
  不受控制(反之亦然)。决定使用受控还是
  组件生命周期中不受控制的输入元素。


我怀疑这与我如何为这些TextField定义值以及如何为运送属性设置状态有关。我究竟做错了什么?我组件的代码如下:


import React, {Component} from 'react';
    import TextField from 'material-ui/TextField';
    import RaisedButton from 'material-ui/RaisedButton';
    import 'whatwg-fetch';

    class Clients extends Component {
      constructor() {
        super();
        this.state = {
          "clientName": "client name",
          "shipping": {
            "name": "name",
            "address1": "address 1",
            "address2": "address 2",
            "city": "city",
            "state": "state",
            "zip": "zip",
            "country": "country"
          },
          "license": "license"
        };

        this.handleChange = this.handleChange.bind(this);
        this.handleShippingChange = this.handleShippingChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
      };

      handleChange(event) {
        this.setState({
          [event.target.name]: this.getFieldValue(event.target)
        });
      };

      handleShippingChange(event) {
        this.setState({
          shipping: {
            [event.target.name]: this.getFieldValue(event.target)
          }
        });
      };

      getFieldValue(target) {
        return target.type === 'checkbox' ? target.checked : target.value;
      };

      handleSubmit = (event) => {
        event.preventDefault();

        // do some stuff
      };

      render() {
        return <div>
          <h1>
            Clients Page
          </h1>

          <form onSubmit={this.handleSubmit}>
            <TextField
              hintText="Enter the client name"
              floatingLabelText="Client Name"
              value={this.state.clientName}
              onChange={this.handleChange}
              name="clientName"
            />
            <h2>Shipping Info</h2>
            <TextField
              hintText=""
              floatingLabelText="Name"
              value={this.state.shipping.name}
              onChange={this.handleShippingChange}
              name="name"
            />
            <br />
            <TextField
              hintText=""
              floatingLabelText="Address Line 1"
              value={this.state.shipping.address1}
              onChange={this.handleShippingChange}
              name="address1"
            />
            <br />
            <TextField
              hintText=""
              floatingLabelText="Address Line 2"
              value={this.state.shipping.address2}
              onChange={this.handleShippingChange}
              name="address2"
            />
            <br />
            <TextField
              hintText=""
              floatingLabelText="City"
              value={this.state.shipping.city}
              onChange={this.handleShippingChange}
              name="city"
            />
            <br />
            <TextField
              hintText=""
              floatingLabelText="State"
              value={this.state.shipping.state}
              onChange={this.handleShippingChange}
              name="state"
            />
            <br />
            <TextField
              hintText=""
              floatingLabelText="Zip Code"
              value={this.state.shipping.zip}
              onChange={this.handleShippingChange}
              name="zip"
            />
            <br />
            <TextField
              hintText=""
              floatingLabelText="Country"
              value={this.state.shipping.country}
              onChange={this.handleShippingChange}
              name="country"
            />
            <br />
            <TextField
              hintText=""
              floatingLabelText="License"
              value={this.state.license}
              onChange={this.handleChange}
              name="license"
            />
            <br />
            <RaisedButton label="OK" primary={true} type="submit" />
          </form>
        </div>
      };
    }

    export default Clients;

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

最佳答案

受控/不受控制的输入表示<input>字段是否具有值。

// This is a controlled input
<input value="foo"/>

// This is an uncontrolled input
<input value={null}/>


这个想法是您不想从受控输入更改为非受控输入。两种类型的输入行为不同,这可能导致错误和/或不一致。

最简单的解决方法是确保始终有一个默认值(在为空字段的情况下,默认值为空字符串'')。

另外,请注意,如果您对某个值的类型有保证,则一致类型通常比可空类型更好。这有助于减少空检查(if (val != null) { /* ...etc */ })造成的开销

但是,如果您只想进行单行修复,则还可以在jsx中提供默认值inline:

<input value={value || ''}/>

关于reactjs - react 形式错误将文本类型的受控输入更改为不受控制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42606621/

10-10 15:00