这是我render
的FormEl
组件方法。
return <div>
<button type="button" onClick={this.clicked}>click to add 1 input</button>
<Inputs count={this.state.childcount}/>
</div>
在clicked
方法中,我childcount
使用 1更新setState
这是Inputs
课
class Inputs extends React.Component {
constructor(props) {
super(props);
console.log("Input constructor is running.");
this.state = {value: '', p: "p", count: props.count};
}
render() {
let c = [];
for (let i = 0; i < this.state.count; i++) {
c.push(
<div key={"id-" + (i * 10)}>
<input type="text" defaultValue={this.state.value}/>
<p> {this.state.p}</p>
</div>);
}
return <div>
{c}
</div>;
}
}
但是,当我更新childcount
in 时clicked
,我可以看到FormEl
重新渲染,count
in<Inputs..
相应增加。但它不会调用除第一次(页面加载)时间之外constructor
的其他时间。Inputs