import React from "react";
function Api(){
return(
fetch('http://universities.hipolabs.com/search?country=Pakistan')
.then((api_data) => {
return api_data.json();
})
.then((json_data) => {
for(let i=0; i<json_data.length; i++){
<div style={{borderWidth:2}}>
Serial No: {i} <br />
Name of University: {json_data[i].name} <br />
Province Code: {json_data[i].stateProvince} <br />
Country: {json_data[i].country} <br />
Country Code: {json_data[i].alpha_two_code} <br />
URL Domain: {json_data[i].domains[0]} <br />
URL: {json_data[i].web_pages[0]} <br />
</div>
}
})
.catch((error) => {
console.log(error);
})
);
}
export default Api;
I want to get dispaly from the above code, but it is showing a blank screen.
3
Answers
you need to use useEffect and useState
and Api must return a valid jsx not response
this worked fine!
You are not returning the div element,try to return it
You need to add return here
but better if you will save API call result in result state and after that you can render it using map.