gpt4 book ai didi

reactjs - react : Why component's constructor is called only once?

转载 作者:行者123 更新时间:2023-12-03 12:56:12 24 4
gpt4 key购买 nike

following example ,当单击 Item 2 时,将显示 Second 1 而不是 Second 2。为什么?你会如何解决这个问题?

<小时/>
var guid = 0;

class Content extends React.Component {
constructor() {
guid += 1;
this.id = guid;
console.log('ctor', this.id); // called only once with 1
}
render() {
return (
<div className="content">
{this.props.title} - {this.id}
</div>
);
}
}

class MyApp extends React.Component {
constructor() {
this.items = ['Item 1', 'Item 2'];
this.state = {
activeItem: this.items[0]
};
}
onItemClick(item) {
this.setState({
activeItem: item
});
}
renderMenu() {
return (
<div className="menu">
<div onClick={this.onItemClick.bind(this, 'Item 1')}>Item 1</div>
<div onClick={this.onItemClick.bind(this, 'Item 2')}>Item 2</div>
</div>
);
}
renderContent() {
if (this.state.activeItem === 'Item 1') {
return (
<Content title="First" />
);
} else {
return (
<Content title="Second" />
);
}
}
render() {
return (
<div>
{this.renderMenu()}
{this.renderContent()}
</div>
);
}
}

最佳答案

React 的 reconciliation algorithm假设在没有任何相反信息的情况下,如果自定义组件出现在后续渲染中的同一位置,则它与之前的组件相同,因此重用前一个实例而不是创建新实例。

如果您要实现componentWillReceiveProps(nextProps) ,您会看到它被调用。

Different Node Types

It is very unlikely that a <Header> element is going to generate a DOM that is going to look like what a <Content> would generate. Instead of spending time trying to match those two structures, React just re-builds the tree from scratch.

As a corollary, if there is a <Header> element at the same position in two consecutive renders, you would expect to see a very similar structure and it is worth exploring it.

Custom Components

We decided that the two custom components are the same. Since components are stateful, we cannot just use the new component and call it a day. React takes all the attributes from the new component and calls component[Will/Did]ReceiveProps() on the previous one.

The previous component is now operational. Its render() method is called and the diff algorithm restarts with the new result and the previous result.

如果给每个组件 a unique key prop ,React可以使用key更改以推断该组件实际上已被替换,并将从头开始创建一个新组件,从而为其提供完整的组件生命周期。

<小时/>
  renderContent() {
if (this.state.activeItem === 'Item 1') {
return (
<Content title="First" key="first" />
);
} else {
return (
<Content title="Second" key="second" />
);
}
}

关于reactjs - react : Why component's constructor is called only once?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29074690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com