Home > Software design >  React - Use async function for rendering in JSX?
React - Use async function for rendering in JSX?

Time:10-24

How can I use a promise function inside the return of a component, like this:

function MyComp () {
 return (
  <div>
   {getFirebasePromise(ref).then(url => return url)}
  </div>
 )
}

So the result would be:

<div>urlFromFirebase</div>

Do I have to use a useState() even though I have several promises in the same component?

CodePudding user response:

You can use useState and useEffect to do that.

function MyComp () {
    [url, setUrl] = useState("");
    useEffect(()=>{
        getFirebasePromise(ref).then(url => setUrl(url));
    });
    
    return (
        <div>
            {url}
        </div>
    )
}

CodePudding user response:

in this situation you can't use a promise there but there are some several libraries that you can use. At this link you can see an example how to do: Here

  • Related