0

I'm struggled in update an Array containing react components that i also use as state.

This is my parent state:

const [result, setResult] = useState(0)
const [inputList, setInputList] = useState([])

inside inputList i have multiple child components defined like this:

<Row key={calculateId(inputList)} id={calculateId(inputList)} parentCallback={removeRow} updateValue={calculateRes} value={0} sign={'plus'} enabled={true}/>

Now, when input changes on child component, new value will be returned to the parent through a callback, but i can't update state because i can't clone the previous array to set the new one updated. I don't know if this mechanism is the right one, if you have any suggestions please do not hesitate to reply Thank you in advance!

1 Answers1

0

You can use callback function of setState.

For example.

const [inputList, setInputList] = useState([])

// some code

setInputList((prevStae) => {
   return prevState.map(item => {
      // some mutations for item
      return item;
    })
})
Avetik Nersisyan
  • 327
  • 3
  • 13