Reactjs 如何从此函数React/React native返回JSX

Reactjs 如何从此函数React/React native返回JSX,reactjs,react-native,jsx,Reactjs,React Native,Jsx,我有一个函数,我想从中返回JSX。我是个新手,我尝试过: 历史是对象数组,我想从中返回文本并显示它 renderCard=()=>{ const db=firebase.firestore() db.collection(“用户”) .doc(firebase.auth().currentUser.uid) .收款(“用户请求存款”) .get() 。然后(快照=>{ 让history=snapshot.docs.map(doc=>{ const data=doc.data().请求; 返回{…

我有一个函数,我想从中返回JSX。我是个新手,我尝试过: 历史是对象数组,我想从中返回文本并显示它

renderCard=()=>{ const db=firebase.firestore()

db.collection(“用户”)
.doc(firebase.auth().currentUser.uid)
.收款(“用户请求存款”)
.get()
。然后(快照=>{
让history=snapshot.docs.map(doc=>{
const data=doc.data().请求;
返回{…数据};
});
history.forEach(elem=>{
返回(
{elem.data}
);
});
});

})

这是一个很好的关于如何使用React的案例研究

您希望在组件装载时获取此数据。获得数据后,将更新组件的状态。然后可以渲染该状态的输出

以下是如何使用代码执行此操作:

从'react'导入{useffect,useState};
constyourcomponent=()=>{
const[history,setHistory]=useState([]);//将数据存储在此状态
//此useEffect将仅运行一次(当组件装入时)。
useffect(()=>{
db.collection('用户')
.doc(firebase.auth().currentUser.uid)
.collection('userRequestDeposit')
.get()
.then(snapshot=>setHistory(snapshot.docs.map(doc=>({…doc.data().request})));
}, []); //  (
{item.data}
));
};
或者作为类组件

从'react'导入{Component};
类YourComponent扩展组件{
状态={
历史:[],
};
componentDidMount(){
db.collection('用户')
.doc(firebase.auth().currentUser.uid)
.collection('userRequestDeposit')
.get()
。然后(快照=>{
this.setState({history:snapshot.docs.map(doc=>({…doc.data().request}));
});
}
render(){
返回history.map(项=>(
{item.data}
));
}
}

foreach不返回任何内容,请尝试将此数组存储在变量中并返回此变量!基于类的组件是否有等价物?我已经用类组件等价物更新了答案非常感谢,它正在工作:)
db.collection("users")
  .doc(firebase.auth().currentUser.uid)
  .collection("userRequestDeposit")
  .get()
  .then(snapshot => {
    let history = snapshot.docs.map(doc => {
      const data = doc.data().request;
      return { ...data };
    });
 

    history.forEach(elem => {
      return (
        <View>
          <Text>{elem.data}</Text>
        </View>
      );
    });
  });