我正在构建一个主要用React编写的Gatsby应用程序。我有一个LoggedIn组件,可以读取所有user已借用的书并在网站上显示状态。我使用Firebase。我希望LoggedIn我可以在组件中获取书籍。但是我不确定如何等待fetch方法完成。当我使用时async/await,它刚刚坏了,因为然后我的功能组件将返回a Promise而不是JSX:ELEMENT类型。我该如何解决这个问题?
import React, { useState } from 'react' import {fetchUserBook} from "../../firebase/firebaseService" const LoggedIn = ({user}) => { //if I put async before user, //my LoggedIn component will return a promise, not a JSX component, which will break my code. const[books,setBooks] = useState([]) fetchUserRestaurant(user.email).then((info) => setBooks(info)) const renderloggedIn = () =>{ return ( <> <h1>Welcome, {user.email}.</h1> // I hope that I can pass the "books" props here so that I can render it. // But usually the return statement is invoked before my fetchUserRestaurant method finishes. </> ) } return( renderloggedIn() ) } export default LoggedIn ``