React: update one item in a list without recreating all items

前端 未结 6 666
隐瞒了意图╮
隐瞒了意图╮ 2021-01-30 05:27

Let\'s say I have a list of 1000 items. And I rendering it with React, like this:

class Parent extends React.Component {
  render() {
    // this.state.list is a         


        
6条回答
  •  佛祖请我去吃肉
    2021-01-30 05:43

    you have to compare a item with your items

    UpdateComment=(comment)=>{
        let objIndex = this.state.comments.findIndex((obj => obj.id === comment.id));
    
        let comments = [...this.state.comments];
        comments[objIndex].content = comment.content
        comments[objIndex].idea = comment.idea
        this.setState({comments});
      }
    

提交回复
热议问题