gpt4 book ai didi

javascript - 如何在不使用 React 重新渲染整个列表的情况下添加新元素

转载 作者:数据小太阳 更新时间:2023-10-29 05:32:25 25 4
gpt4 key购买 nike

我正在开发简单的流应用程序。我有帖子列表,这个列表可以接收更新,更新将显示在它的顶部。

问题是在接收到每个新帖子时,React 都会重新呈现整个元素列表。我已经为它做了一个简单的例子。

有什么办法可以避免这种行为吗?我在 React 文档上看到了dynamic-children 主题,但在示例中,如您所见,我已经更新了所有子项。

class Post extends React.Component {
render() {
console.log('rerendered post', this.props.reactKey);
return (
<li>{this.props.post.text}</li>
);
}
}

class App extends React.Component {

constructor(props) {
super(props);
this.state = {posts: [
{id: '00001', text: 'First one'},
{id: '00002',text: 'Second one'},
{id: '00003',text: 'Third one'}
]};
}

addPost() {
const posts = this.state.posts;
posts.unshift({id: '00004', text: 'New post'});
this.setState({posts: posts});
}

render() {
return (
<div>
<button onClick={this.addPost.bind(this)}>Add Post</button>
<ul>
{this.state.posts.map((post, index) => {
return (<Post post={post} key={post.id} reactKey={index} />);
})}
</ul>
</div>
);
}
}


ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<body>
<div id="root"></div>
</body>

解决方案

问题是我使用 .map 函数的索引是每个列表组件的键而不是唯一键。并且因为在添加新元素以列出所有索引更改为 +1 后,所以第一篇文章变成了第二篇文章,我所有的文章都重新呈现了。所以首先,检查你是否在所有列表元素中使用了唯一键 :-)

最佳答案

只需要完成一次的工作应该在保证只运行一次的生命周期方法中完成,例如componentDidMount。 .正如文档所建议的那样:

If you want to integrate with other JavaScript frameworks, set timers using setTimeout or setInterval, or send AJAX requests, perform those operations in this method.

我在您的代码段中向 componentDidMount 添加了日志记录,以显示渲染发生多次,但每个实例仅调用一次 componentDidMount

class Post extends React.Component {
componentDidMount() {
console.log('mounted post', this.props.id);
}

render() {
console.log('rerendered post', this.props.id);
return (
<li>{this.props.post.text}</li>
);
}
}

class App extends React.Component {

constructor(props) {
super(props);
this.nextId = 4;
this.state = {
posts: [
{id: 1, text: 'First one'},
{id: 2,text: 'Second one'},
{id: 3,text: 'Third one'},
],
};
}

addPost() {
const posts = this.state.posts;
posts.unshift({id: this.nextId, text: 'Post ' + this.nextId});
this.nextId++;
this.setState({posts: posts});
}

render() {
return (
<div>
<button onClick={this.addPost.bind(this)}>Add Post</button>
<ul>
{this.state.posts.map((post, index) => {
return (<Post post={post} key={post.id} id={post.id} />);
})}
</ul>
</div>
);
}
}


ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<body>
<div id="root"></div>
</body>

关于javascript - 如何在不使用 React 重新渲染整个列表的情况下添加新元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39082597/

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