简体   繁体   中英

Making dynamic form inputs with React

how to update the nested fields in react forms .,

here i do

add a new item handleAddShareholder ,

delete an existing item handleRemoveShareholder ,

change details of an item handleShareholderNameChange

then i will push a new array to shareholders.customize by this

handleAddcomp = idx => () => {
    this.state.shareholders[idx].customize.push({ name: '' });
    const shareholders = this.state.shareholders;
    this.setState({ shareholders: shareholders });
  };

but i can't able to update the field value of nested arrays in react.js

my code is here https://jsbin.com/fugemuy/edit?html,js,output

Use Object.assign for this purpose. It will clone current object.

handleAddcomp = idx => () => {
    let shareholders = Object.assign({}, this.state.shareholders); //creating copy of object in state
    shareholders[idx].customize.push({ name: '' })
    this.setState({ shareholders });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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