My code is going to be quite explanatory in itself.
export default function ViewCustomer() {
const [customer, setCustomer] = useState();
const {id} = useParams();
useEffect(() => {
CustomerService.getCustomerById(id).then(res => {
setCustomer(res.data);
});
}, [id]);
return (
<div>
{customer.first_name}
</div>
);
}
My code gives customer is undefined error. This is happening because useEffect hook is not executing before return is being executed. How and why is this happening and how can I fix this?