简体   繁体   中英

React Variable value is not changing

i'm trying to do something in react and i got stuck .. i don't know why this is happening, i can't explain myself.

let content = null;
storage.ref().child(snapshot.val().content).getDownloadURL().then(url => content = url ); // setting value

console.log('content', content); // returns initial value, in my case, null. why?

Line 19

https://pastebin.com/UkJyJihB

Thanks!

Your action is asynchronous. It means that 'then' function fires only when getDownloadURL() is finished. But console.log fires immidiately, when the content is null yet. So if you want to do something with content, you should do it inside 'then' callback:

let content = null;
storage.ref().child(snapshot.val().content).getDownloadURL()
.then(url => {
   content = url; 
   console.log('content', content);
} ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM