React Variable value is not changing

谁都会走 提交于 2020-03-27 07:14:24

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/45649768/react-variable-value-is-not-changing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!