用条件事件目标来反应setState对象键

时间:2019-03-24 17:02:19

标签: reactjs object event-handling setstate

我在将对象键设置为状态时遇到问题。因此,下面的代码是带有标题,正文,城市和国家的表格。使用用户给定的值,将它们设置为state。但是由于应该在“地址”对象中分配国家和城市,所以我创建了一个处理程序,检查该值是否为国家/城市。

但是事实证明,即使不满足条件并且ELSE正在运行,它仍然会使用if条件中分配的对象。

因此,无论是标题还是城市,都在地址对象内设置了州

您会检查我在哪里做错了吗?

下面是我的表单和处理程序

class Postit extends Component {
  state = {
    title: "",
    body: "",
    address: {
      country: "",
      city: ""
    }
  };

  handleChange = e => {
    if (e.target.id === "country" || "city") {
      let address = Object.assign({}, this.state.address); 
      address[e.target.id] = e.target.value; 
      this.setState({ address });
      console.log("NewState", this.state);

    } else {
      console.log("Target is not city or country");
     this.setState({
        [e.target.id]: e.target.value
      });
    }
  };

  handleSubmit = e => {
    e.preventDefault();
    console.log(this.state);
  };
  render() {
    return (
      <div className="container">
        <form className="white" onSubmit={this.handleSubmit}>
          <h5 className="grey-text text-darken-3">Send your post</h5>
          {/* Title */}
          <div className="input-field">
            <input type="text" id="title" onChange={this.handleChange} />
            <label htmlFor="title"> Title</label>
          </div>
          {/* Body */}
          <div className="input-field">
            <textarea
              id="body"
              className="materialize-textarea"
              onChange={this.handleChange}
            />
            <label htmlFor="body"> Content</label>
          </div>
          {/* City / Country Select */}

          <div className="input-field">
            <input type="text" id="country" onChange={this.handleChange} />
            <label htmlFor="country"> Write your Country</label>
          </div>

          <div className="input-field">
            <input type="text" id="city" onChange={this.handleChange} />
            <label htmlFor="city"> Write your City</label>
          </div>

          <div className="input-field">
            <button
              className="btn pink lighten-1 center-align"
              style={{ marginTop: "10px", borderRadius: "6px", width: "100%" }}
            >
              Post it
            </button>
          </div>
        </form>
      </div>
    );
  }
}

示例,当我填写标题时,newState看起来像这样:

{title: "", body: "", address: {country:"", city:"", title:"test"}}

谢谢!

1 个答案:

答案 0 :(得分:0)

您没有正确检查条件。即使if (e.target.id === "country" || "city")不是“国家”,由于e.target.id是真实值,所以"city"总是正确的。应该是if (e.target.id === "country" || e.target.id === "city")

 handleChange = e => {
    if (e.target.id === "country" || e.target.id === "city") {
      let address = Object.assign({}, this.state.address); 
      address[e.target.id] = e.target.value; 
      this.setState({ address });
      console.log("NewState", this.state);

    } else {
      console.log("Target is not city or country");
     this.setState({
        [e.target.id]: e.target.value
      });
    }
  };

希望这会有所帮助!