React setState object 键与条件事件目标
React setState object key with conditional event target
我在设置 object 键状态时遇到问题。所以下面的代码是一个带有标题 body、城市和国家的表格。根据用户给出的值,我将它们设置为状态。但是由于国家和城市应该在 'address' object 中分配,我创建了一个处理程序来检查值是否为 country/city。
但结果即使不满足条件,ELSE是运行,仍然使用if条件中的object赋值
所以无论是标题还是城市,州都设置在地址中 object
你能检查一下我哪里做错了吗?
下面是我的表单和处理程序
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"}}
谢谢!
您没有正确检查条件。 if (e.target.id === "country" || "city")
永远为真,即使 e.target.id
不是 "country",因为 or 部分 "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
});
}
};
希望对您有所帮助!
我在设置 object 键状态时遇到问题。所以下面的代码是一个带有标题 body、城市和国家的表格。根据用户给出的值,我将它们设置为状态。但是由于国家和城市应该在 'address' object 中分配,我创建了一个处理程序来检查值是否为 country/city。
但结果即使不满足条件,ELSE是运行,仍然使用if条件中的object赋值
所以无论是标题还是城市,州都设置在地址中 object
你能检查一下我哪里做错了吗?
下面是我的表单和处理程序
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"}}
谢谢!
您没有正确检查条件。 if (e.target.id === "country" || "city")
永远为真,即使 e.target.id
不是 "country",因为 or 部分 "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
});
}
};
希望对您有所帮助!