好的,我已经知道一种方法可以做到这一点。但是,我问这个是为了防止我重新发明轮子,因为我是 React 的新手。我的印象是,如果父组件通过 props 将她的状态传递给子组件,而不是更新父组件的状态,则子组件将在需要时重新渲染。但事实似乎并非如此。我设置了这个例子,
class Child extends Component {
constructor(props) {
super(props);
this.state = {
number: props.number,
};
}
updateNumber(n) {
this.setState({number: n});
}
render() {
return (<h1>{this.state.number}</h1>);
}
}
class Parent extends Component {
constructor(props) {
super(props);
this.state = {
number: -1,
};
this.child = React.createRef();
setInterval(this.updateState.bind(this), 1000);
}
updateState() {
console.log(this.state);
this.setState({
number: Math.floor((Math.random() * 10) + 1),
});
// this.child.current.updateNumber(this.state.number);
}
render() {
return (
<div>
<Child ref={this.child} number={this.state.number}/>
</div>
);
}
}
在这个例子中,除非我显式定义一个引用并使用它来调用子项的更新函数(注释部分),否则每次更新父项的状态时都不会重新渲染子项。是这样吗?如果他们的父母的状态作为道具传递给他们,你是想手动更新你的孩子的状态(嘿)还是他们应该自动更新(并因此重新渲染)。
原文由 scribe 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是因为您的 Child 组件也在使用自己的状态。总是问自己
state
是否必要,这是 React 初学者的常见错误。希望你能通过看这个例子更深入地理解它。 Instead of calling
setInterval
in your constructor function, I will recommend you to call it incomponentDidMount
and toclearInterval
in yourcomponentWillUnmount
.