简体   繁体   English

反应变量值没有改变

[英]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 19号线

https://pastebin.com/UkJyJihB https://pastebin.com/UkJyJihB

Thanks! 谢谢!

Your action is asynchronous. 您的操作是异步的。 It means that 'then' function fires only when getDownloadURL() is finished. 这意味着仅当getDownloadURL()完成时才触发“ then”功能。 But console.log fires immidiately, when the content is null yet. 但是,当内容为null时,console.log将立即触发。 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);
} ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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