reactjs - 离子反应组件仅在单击按钮时显示
问题描述
我是 Ionic 和 React 的新手,想构建一个移动应用程序。该应用程序的 UI 结构并不新,我有一个主屏幕,其作用类似于提要并显示从 Firebase 获取的数据。问题是在打开页面时,负责显示数据的组件<Requests/>
未呈现。为了做到这一点,我必须点击一个按钮——第一个显示模式的<IonFabButton>
内部按钮——<IonFabList>
这对我来说没有任何意义。谢谢。
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonTitle>Requêtes de Livraison</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<Requests requests={deliveryRequests} />
<IonModal isOpen={showModal}>
<h2>Créer une nouvelle requête de livraison</h2>
<IonInput value={newDeliveryRequestTitle} placeholder="Entrer un titre..." onIonChange={e => setNewDeliveryRequestTitle(e.detail.value!)}></IonInput>
<IonInput value={newDeliveryRequestDescription} placeholder="Entrer une description..." onIonChange={e => setNewDeliveryRequestDescription(e.detail.value!)}></IonInput>
<IonButton onClick={() => { setShowModal(false); }}>Fermer</IonButton>
<IonButton onClick={() => {
createNewDeliveryRequest({
title: newDeliveryRequestTitle,
description: newDeliveryRequestDescription,
followers: 0
}); setShowModal(false);
}}>Créer</IonButton>
</IonModal>
<IonFab vertical="bottom" horizontal="end" slot="fixed">
<IonFabButton><IonIcon icon={chevronUpCircleOutline} /></IonFabButton>
<IonFabList side="top">
<IonFabButton onClick={() => setShowModal(true)}><IonIcon icon={addCircleOutline} /></IonFabButton>
<IonFabButton><IonIcon icon={navigateCircleOutline} /></IonFabButton>
</IonFabList>
</IonFab>
</IonContent>
</IonPage>
);
解决方案
好吧,我的问题的解决方案很简单,原因是函数中的一个愚蠢的错误:
const fetchDeliveryRequests = () =>{
let requests = Array<DeliveryRequest>();
db.collection('delivery-requests').get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
let deliveryRequest: DeliveryRequest = {
title: doc.data().title,
description: doc.data().description,
followers: doc.data().followers,
};
requests.push(deliveryRequest);
});
});
setDeliveryRequests(requests); //It should not be here
}
我将其更改为:
const fetchDeliveryRequests = () =>{
let requests = Array<DeliveryRequest>();
db.collection('delivery-requests').get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
let deliveryRequest: DeliveryRequest = {
title: doc.data().title,
description: doc.data().description,
followers: doc.data().followers,
};
requests.push(deliveryRequest);
});
setDeliveryRequests(requests); //New place
});
}
说明: setDeliveryRequests() 在 get() 方法之外/之后被调用。它没有时间向前传递数据。
推荐阅读
- jsp - 如何访问 display:column 的属性并正确使用 IF/WHEN 条件
- python - ImportError:使用 docker 时没有名为 appengine.ext 的模块
- php - 分页在自定义帖子类型中不起作用
- javascript - webpack-dev-server:tampermonkey 脚本的自动重新加载
- python - python spotify api获取访问令牌和刷新令牌给400
- javascript - JS函数中的EJS数组
- github - 从测试到开发的 Azure 数据工厂升级,身份验证方法是 Azuressql 的托管标识,使用 azure devops CI/CD
- rest - 如何避免 Golang 服务器(Gin Gonic)在 INTERNAL_ERROR 上崩溃
- c# - 资源和 x:Bind 的奇怪行为
- javascript - 在反应字段中获取自动填充浏览器数据的最佳实践是什么?