Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print async data in react

this is my code and print is a page
and getdata is a async function that returns a promise

const print = () => {
                      getdata.then((res)=>{
                         return (<p>{res}</p>)
                      })

return(
        <>
           <div>{getdata()}</div>
        </>
      )
}
export default print;

how can I print data in page

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

//Use a state to store fetched data
const [data, setData] = useState('');

//useEffect to fecth data on first render
useEffect(()=>{
  //Self calling async function
  (async()=>{
    let fetched = await getdata()
    let json = await fetched.json()
    setData(json)
  })()
},[])

return(
        <>
           <div>{data}</div>
        </>
      )
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading