I'm new to React.js On the First Time load Code shows an error when i comment this part {data.advice} and run it shows me html it also calls API 2 times it gives an Error on Reload. how to fix it?
import { Box, Button, Card, CardActions, CardContent, Container, Typography } from '@mui/material';
import axios from 'axios';
import React, { useEffect, useState } from 'react';
const Advice = () => {
const [data, setData] = useState(null);
const clicked = () => {
callAPI();
}
const callAPI = () => {
axios.get('https://api.adviceslip.com/advice')
.then(respone => {
setData(respone.data.slip)
console.log(respone.data.slip);
})
.catch((error) => {
console.log(error);
})
}
useEffect(() => {
callAPI();
}, [])
return (
<Box>
<Container>
<Typography variant='h2' sx={{ textAlign: "center" }}>Advice App</Typography>
<Card sx={{ maxWidth: 500, margin: "10px auto", display: "block", textAlign: "center" }}>
{/* <CardMedia
sx={{ height: 140 }}
image="/static/images/cards/contemplative-reptile.jpg"
title="green iguana"
/> */}
<CardContent>
<Typography gutterBottom variant="h5" component="div">
Advice of the Day
</Typography>
<Typography variant="body2" color="text.secondary">
{data.advice}
</Typography>
</CardContent>
<CardActions>
<Button size="small" onClick={clicked}>Share</Button>
<Button size="small">Learn More</Button>
</CardActions>
</Card>
</Container>
</Box>
);
};
export default Advice;
https://codesandbox.io/s/cool-wildflower-p7k6ee?file=/src/App.js
Any Help will be Appreciated