I am trying to update an array that is in state on a functional component. I have tried making a copy of the array... pushing to the copy... and then setArray(newarray)...
The array is not updating on screen or in console.log until the NEXT update to the array. So, I am always one array item behind. This is on React Native by the way, using Typescript.
Am I doing something wrong here? How do I get componentDidUpdate to trigger when the state array is modified?
Here is the code I am using:
const [currentNumber, setCurrentNumber] = useState<string[]>([]);const numberAppended = (number: string) => { setCurrentNumber(currentNumber => [...currentNumber, number])}
I've also tried
const [currentNumber, setCurrentNumber] = useState<string[]>([]); const numberAppended = (number: string) => { let newCurrentNumber = [...currentNumber]; newCurrentNumber.push(number); setCurrentNumber(newCurrentNumber);}