I am trying to output a single value of an object but am getting null. Don't know where I got it wrong. Below is the JSON response I got but when I tried to display it, am geting null ;
{
"success": true,
"message": "Course Retrieved Successfully.",
"data": {
"id": 1,
"course_code": "null",
"course_name": "Greatness",
"slug": "greatness",
"course_fee": 10,
"duration": "10hours",
"start_date": "2000-09-08",
"end_date": "2000-09-08",
}
}
My code below
const [coursedetails, setCourseDetails] = useState([])
const init =()=>{
axios.get('http://example.com/api/courses').then(response => {
setCourseDetails(response);
}).catch(error =>{
console.log('error',error)
})
}
useEffect(() => {
init();
}, []);
return (
<div>
{coursedetails.data.course_name}
</div>
)
responsevariable value?fetchinstead of axios. unless you're intercepting any requests or making pre and post flight confligs then you really dont need axios.fetch('http://example.com/api/courses').then(r => r.json()).then((response) => { setCourseDetails(response); } )