I have just started learning React and am struggling to create a component which calls and API and then presents the returned data on the page.
I have using state and am trying to update the state after calling my API which will then update the JSX code so the returned API data is presented on the browser.
Here is the code for my component
import { useParams } from "react-router";
const axios = require('axios');
const DEALS_API_URL = "";
const Deal = () => {
let { deal_id } = useParams();
const [deal, setDeal] = useState({});
async function getDeal() {
await axios.get(DEALS_API_URL + deal_id, {})
.then(res => {
setDeal(res.data);
});
}
useEffect(() => {
getDeal();
}, []);
return (
<div>
<p>{deal.title}</p>
</div>
);
}
export default Deal;
Currently the page is loaded and the deal title is not displayed, how can I load the page and then when the state is updated after calling my API present the deal title on the browser