I want to retrieve a field (for now) from the promise and render it as the title in my pop-up modal. I got the promise to be resolved by using useEffect
and it does populate my modal, however there is a slight delay for it to appear as it waits for the response to be set a value.
I tried playing around with creating another function below Modal in the same file to use aync/await
, however I'm stumped in figuring out where to place them, as I keep getting errors for using it incorrectly, or nothing from the promise is passed back.
import React, { useState, useEffect } from 'react';
import './Modal.css';
const Modal = ({close, item}) => {
const [response, setResponse] = useState(null);
useEffect(() => {
item.then(object => setResponse(object.data.title))
}, [])
return (
<div className='modal'>
<div className='popup'>
<h1>{response}</h1>
<button onClick={ () => { close(false) }}>close me</button>
</div>
</div>
);
}
export default Modal;