【发布时间】:2020-01-22 21:53:31
【问题描述】:
我有以下代码。我正在使用 setState 方法 (this.setState({ country: "", offset: "" });) 设置状态,它不会导致 render() 方法自行重新呈现。
state = { country: "", offset: "" };
onClicked = () => {
console.log("Fine Click away");
// Get the call back from props
let country = this.state.country;
let offset = this.state.offset;
this.setState({ country: "", offset: "" });
const callback = this.props.onpress;
if (callback) {
callback(country, offset);
}
};
getTheCountryName = event => {
this.setState({ country: event.target.value });
};
getTheOffSet = e => {
this.setState({ offset: e.target.value });
};
render() {
const Caption = this.props.caption;
return (
<div>
<br></br>
<br></br>
<div>Please enter the name of country and offset:</div>
<input
placeholder="Name of Country"
onChange={this.getTheCountryName}
/>
<input placeholder="offset" onChange={this.getTheOffSet} />
<br></br>
<br></br>
<button className="rectangular-button" onClick={this.onClicked}>
<div className="caption">{Caption}</div>
</button>
</div>
);
}
}```
【问题讨论】:
-
不确定为什么要复制状态属性的值。
-
你怎么知道
render没有被调用?您不会在组件的state中显示值。 -
您的代码确实触发了
render调用:codesandbox.io/s/divine-cloud-vv3ob
标签: javascript html css reactjs jsx