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

React Native, pushing items to an array state not working

$
0
0

I need to push items to my array, but when I console.log this array, it says 'undefined' for the specific value. I'm trying btw to fetch data from firebase storage. How do I correctly add items to an Array

Here is my code:

const [imagelinks, setImagelinks] = React.useState(['']);const myFunction = () =>{  await storage()      .ref(`${_userid}`)      .list()      .then(result => {        result.items.forEach(async ref => {          await storage()            .ref(ref.fullPath)            .getDownloadURL()            .then(url => {              //get url              setImagelinks([...imagelinks, url]);              console.log('Links: '+ url);            });        });       //there it says undefined when logging...       console.log(imagelinks[0])      });}

Edit: can I use following?

const imagelinks = [];//instead of const [imagelinks, setImagelinks] = useState([]);

Viewing all articles
Browse latest Browse all 6287

Trending Articles



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