Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

React Component not Rerending When Updating State Array

$
0
0

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);}

Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>