reactjs - React - map a list in state does not properly affect the UI -
i have list in state of parent component:
this.state = { productsform: [], } and perform map on list , call child component:
renderproducts() { const result = this.state.productsform.map((value, index) => ( <productform index={index} onchange={(name, form, strength) => { const productsform = this.state.productsform; productsform[index] = { name, form, strength }; this.setstate({ productsform }); }} onremove={(id) => { const filtered = this.state.productsform.filter((_, i) => !== id); this.setstate({ productsform: filtered }); }} /> )); return result; } in child component have remove button returns "id" of parent:
<floatingactionbutton onclick={() => { onremove(this.state.id); }} > it works , remove child parent component, in ui, (i mean html page in browser) there problem. when ever remove child lower index, although removed state, remains in ui!
i think there problem in "index" or in code. have recommendation me fix issue?
you forgot add key attribute each productform component. way, react knows components have been changed.
<productform key={index} ... /> it's better if use product's id key instead of index.
Comments
Post a Comment