2017-01-02 44 views
0

你能告诉我如何使用反应更新数组中的项目吗?我使用add按钮制作动态列表。在生成的项目中,我有两个按钮updatedelete。 点击更新按钮后,我将添加按钮的文本更改为update,并在输入字段中填充所选值。现在当我点击更新按钮时,我想更新所选项目。如何使用反应更新数组中的项目?

这里是我的代码 https://plnkr.co/edit/bpSGPLLoDZcofV4DYxPe?p=preview

addName() { 
    if (this.state.username !== '') { 
     if (this.state.btnText === 'add') { 
      this.props.add(this.state.username) 
     } else if (this.state.btnText === 'update') { 
      this.props.updateItem(this.state.username) 
     } 
     this.setState({ 
      username: '', 
      btnText: 'add' 
     }) 
    } 
} 
delete(item) { 
    this.props.deleteItem(item) 
} 
    update(item){ 
    this.setState({ 
     username : item, 
     btnText:'update' 
    }) 
    } 
+0

您在plunkr代码不包含任何更新按钮 –

+0

请填写输入值,然后单击'add'按钮。然后你在你所提供的链接plunkr看到更新按钮 – user5711656

+0

都能跟得上它不存在。 –

回答

0

您需要将更新后的项目名称和的indexOf项目在减速更新情况。然后只需使用拼接来更新新值

更新了plunkr。检查它here

case 'UPDATE_ITEM': 
     let newList = state.slice() 
     newList.splice(action.payload.index, 1, action.payload.item) 
     return newList; 
相关问题