即使在文本字段中具有值后,也会显示该值为空

时间:2017-06-06 16:00:06

标签: javascript reactjs redux immutable.js

我有一个表单,从服务器获取数据并显示在文本字段中。虽然数据在文本字段中,但当我单击“保存”按钮时,我在显示以下字段的所有字段上都会出错,但不能为空。此外,当我尝试更改值时,我无法输入任何文本也不能删除文本。为什么价值在那边有价值时显示为空?

const mapStateToProps = createStructuredSelector({
  cloudinaryData: selectCloudinary()
});

class Cloudinary extends React.Component {
  state = {
    cloudinary: {
      cloudinary_cloud_name: "",
      cloudinary_api_key: "",
    },
    errors: {}
  };

  componentDidMount() {
    this.props.loadCloudinary();
  }

  handleChange = event => {
    const fieldName = event.target.name;
    this.setState(
      {
        cloudinary: {
          ...this.state.cloudinary,
          [event.target.name]: event.target.value
        }
      },
      () => {
        this.validateField([fieldName]);
      }
    );
  };


  handleSubmit = event => {
    event.preventDefault();
    this.props.requestCloudinary(this.state.cloudinary);
  };

  render() {
    const { cloudinary, errors } = this.state;
    const { cloudinaryData } = this.props;
    if (cloudinaryData.size === 0) {
      return <div>Fetching Data...</div>;
    }
    return (
      <form onSubmit={this.handleSubmit}>
        <TextFieldGroup
          name="cloudinary_cloud_name"
          type="text"
          value={
            cloudinaryData
              ? cloudinaryData.cloudinary_cloud_name
              : cloudinary.cloudinary_cloud_name
          }
          onChange={this.handleChange}
          onBlur={this.handleBlur}
          error={errors.cloudinary_cloud_name}
        />
        <button className="btn btn-primary">Save changes</button>
      </form>
    );
  }
}

1 个答案:

答案 0 :(得分:0)

这是因为你在这里将值设置为props而不是你的状态:

<TextFieldGroup
  name="cloudinary_cloud_name"
  type="text"
  value={
    cloudinaryData
      ? cloudinaryData.cloudinary_cloud_name
      : cloudinary.cloudinary_cloud_name
  }
  onChange={this.handleChange}
  onBlur={this.handleBlur}
  error={errors.cloudinary_cloud_name}
/>

相反,创建一个构造函数并将状态初始化为props并将值替换为使用状态:

const mapStateToProps = createStructuredSelector({
  cloudinaryData: selectCloudinary()
});

class Cloudinary extends React.Component {

  constructor(props){
      super(props);
      state = {
          cloudinary: {
            cloudinary_cloud_name: props.cloudinaryData.cloudinary_cloud_name,
            cloudinary_api_key: props.cloudinaryData.cloudinary_api_key,
       },
       errors: {}
     }
  }

  componentDidMount() {
    this.props.loadCloudinary();
  }

  handleChange = event => {
    const fieldName = event.target.name;
    this.setState(
      {
        cloudinary: {
          ...this.state.cloudinary,
          [event.target.name]: event.target.value
        }
      },
      () => {
        this.validateField([fieldName]);
      }
    );
  };


  handleSubmit = event => {
    event.preventDefault();
    this.props.requestCloudinary(this.state.cloudinary);
  };

  render() {
    const { cloudinary, errors } = this.state;
    const { cloudinaryData } = this.props;
    if (cloudinaryData.size === 0) {
      return <div>Fetching Data...</div>;
    }
    return (
      <form onSubmit={this.handleSubmit}>
        <TextFieldGroup
          name="cloudinary_cloud_name"
          type="text"
          value={cloudinary.cloudinary_cloud_name}
          onChange={this.handleChange}
          onBlur={this.handleBlur}
          error={errors.cloudinary_cloud_name}
        />
        <button className="btn btn-primary">Save changes</button>
      </form>
    );
  }
}