反应变量值没有改变 [英] React Variable value is not changing

查看:58
本文介绍了反应变量值没有改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做些反应,但被卡住了..我不知道为什么会这样,我无法解释自己.

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?

第19行

https://pastebin.com/UkJyJihB

谢谢!

推荐答案

您的操作是异步的.这意味着仅当getDownloadURL()完成时才触发"then"功能.但是,当内容为null时,console.log将立即触发.因此,如果您想对内容进行处理,则应在然后"回调中进行处理:

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

这篇关于反应变量值没有改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆