简体   繁体   English

React.js将新元素推送到嵌套状态

[英]Reactjs push new element to nested state

I'm new to javascript and react, I try to push a new element to an array inside the state but there's no success. 我是javascript新手,但会做出反应,我尝试将新元素推入状态内的数组,但没有成功。

state = {
 columns: [
  {
   id: 122,
   items: [{text:'abc'},{text:'cde'}]
  },
  {
   id: 143,
   items: []
  }
 ]
 }

   addItem(columnId,text) {
     const newItem = {text: text}
   //this.setState(...)
   }

Basically, I have an addItem function with given columnId and some text content, I want to push a new item to the items array inside the column with given columnId. 基本上,我有一个具有给定columnId和一些文本内容的addItem函数,我想将一个新项目推送到具有给定columnId的列内的items数组中。

I heard that it'd be much easier with the help of immutability-helper, is that right? 我听说在不可变帮助程序的帮助下会容易得多,对吗?

You can get value from state and push to that. 您可以从状态中获取价值并将其推向目标。

And this.setState makes re-rendering. 并且this.setState进行重新渲染。

addItem(columnId, text) {
    const newItem = {text};   
    let columns = this.state.columns;
    let findColumn = columns.find(({id})=>id === columnId);
    if( findColumn ) {
        findColumn.items.push( newItem );
    }
    else {
        columns.push({id:columnId, items:[newItem]});
    }
    this.setState({columns});
}

If you want tight. 如果要紧。 We can use destructuring. 我们可以使用解构。

addItem(columnId, text) {
    let {columns} = this.state;
    let findColumn = columns.find(({id})=>id === columnId);
    if( findColumn ) {
        findColumn.items.push( {text} );
    }
    else {
        columns.push({id:columnId, items:[{text}]});
    }
    this.setState({columns});
}

You don't need any immutability helper if you learn methods like map , filter and spread syntax or Object.assign . 如果您学习诸如mapfilterspread语法Object.assign之类的方法,则不需要任何不变性助手。 Using some of them (the suitable ones) you can do whatever you want without mutating your state. 使用其中一些(合适的),您可以做任何您想做的事情而不会改变状态。

const addItem = (columnId, text) => {
  // We are mapping the columns from the state.
  const newColumns = this.state.columns.map(column => {
    // If id does not match just return the column.
    if (column.id !== columnId) return column;
    // Else, return a new column object by using spread syntax.
    // We spread the column (preserve other properties, and create items again
    // using spread syntax. Spread the items, add the new text object.
    return { ...column, items: [...column.items, { text }] };
  });
  // Lastly, set the state with newColumns.
  this.setState({ columns: newColumns });
};

Without comments: 没有评论:

const addItem = (columnId, text) => {
  const newColumns = this.state.columns.map(column => {
    if (column.id !== columnId) return column;
    return { ...column, items: [...column.items, { text }] };
  });
  this.setState({ columns: newColumns });
};

You can create a copy of the state and modify it: 您可以创建状态的副本并进行修改:

addItem(columnId,text) {
   let newColums = [...this.state.columns]; // Create a copy of the state using spread operator
   newColumns[columnId].items.push({text: text}) // Add the new item
   this.setState({columns:newColumns}) // Set the state
}
 addItem(columnId,text) {
    const { columns } = this.state;
    let newItem = columns.find( column => column.columnId === columnId);
    if(newItem) {
        newItem = {
          ...newItem,
          text: text
       }
    } else {
      newItem = {
        columnId: columnId,
        text: text
      }

    }
    const newColumns = [ ...columns, newItem]
    this.setState({ columns: newColumns })
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM