javascript - 如何在jsx中调用变量
问题描述
我已经定义了一个变量,并且我正在设置变量的内容 = 图像 url,如下所示:
var regData = ""
firestore.collection("profiledata").doc(user.uid).get().then((doc) => {
console.log(doc.data().registeringFlag);
regData = doc.data().registeringFlag;
console.log("reg data " + regData)
if (regData == "yes"){
regData = "https://i.gifer.com/Z23Y.gif"
firestore.collection('profiledata').doc(user.uid).set({
registeringFlag: "no"
})
}
})
然后我试图访问regData
我的 jsx 代码中的内容,如下所示:
return (
<BottomTabsWithFullHeightContent onChange={ (activeKey) => pushHistoryForTab(props.history, activeKey) } activeKey={props.showTab}>
<div className="container"><img src={ regData } width="300px" height="300px"/></div>
</BottomTabsWithFullHeightContent>
)
}
但是,它实际上并没有像预期的那样在屏幕上显示图像。它给了我:
TypeError: null is not an object
我该如何解决?
这是 .then 的尝试:
var regData = ""
firestore.collection("profiledata").doc(user.uid).get().then((doc) => {
console.log(doc.data().registeringFlag);
regData = doc.data().registeringFlag;
console.log("reg data " + regData)
if (regData == "yes"){
regData = '"https://i.gifer.com/Z23Y.gif"'
firestore.collection('profiledata').doc(user.uid).set({
registeringFlag: "no"
})
}
}).then (() => {
return (
<BottomTabsWithFullHeightContent onChange={ (activeKey) => pushHistoryForTab(props.history, activeKey) } activeKey={props.showTab}>
<TabPane tab={ <VerticalCaptionedIcon type="contacts" theme="filled" caption="Clients" /> } key={ routes.CLIENTS }>
<ClientTabContent { ...props }/>
</TabPane>
<TabPane tab={ <VerticalCaptionedIcon type="file-text" theme="filled" caption="Estimates" /> } key={ routes.ESTIMATES }>
<EstimateTabContent { ... props }/>
</TabPane>
<TabPane tab={ <VerticalCaptionedIcon type="form" caption="Jobs" /> } key={ routes.JOBS }>
<JobTabContent { ... props }/>
</TabPane>
<TabPane tab={ <VerticalCaptionedIcon type="user" caption="Profile" /> } key={ routes.PROFILE }>
<UserProfileContent { ... props }/>
</TabPane>
<div className="container"><img src={ regData }width="300px" height="300px"/></div>
{ console.log("rd: " + regData) }
</BottomTabsWithFullHeightContent>
)
})
}
解决方案
您需要使用组件级状态来操作动态异步数据。这是您的代码的修改版本。
import {useState} from 'react';
const [regData, setRegData] = useState('');
firestore.collection("profiledata").doc(user.uid).get().then((doc) => {
setRegData(doc.data().registeringFlag);
if (regData == "yes"){
setRegData("https://i.gifer.com/Z23Y.gif");
firestore.collection('profiledata').doc(user.uid).set({
registeringFlag: "no"
})
}
})
return (
<BottomTabsWithFullHeightContent onChange={ (activeKey) => pushHistoryForTab(props.history, activeKey) } activeKey={props.showTab}>
{regData && <div className="container"><img src={ regData } width="300px" height="300px"/></div>}
</BottomTabsWithFullHeightContent>
)
}
推荐阅读
- reactjs - 测试:在带有 react-testing-library 的 react-router-dom 中的链接上单击(fireEvent.click)不会触发
- linux-mint - 我想更改有关 python 的别名,但我不知道如何
- javascript - 如何根据其他数组中的给定顺序对集合进行排序
- javascript - 向对象添加新元素后,我不断收到空值
- smartcard - 带多个插槽的智能卡读卡器 PCSC
- c# - Selenium ChromeDriver - 对远程 WebDriver 服务器的 URL 的 HTTP 请求在 60 秒后超时
- c++ - 为什么在缺少宽度参数时 fprintf 会导致内存泄漏并且行为不可预测
- php - 如果最大值只能是 30,如何组合数组中的值?
- node.js - React Native 0.57.8 但 React Native Navigation 版本是:1.1.471,但 React Native Navigation 的当前版本是:2.20.2
- javascript - 车把 如果 var 长度大于