React/JavaScript新手。花了一整天的时间更改我的API调用,使其显示加载状态,这样页面就不会呈现尚未调用的API数据,但我得到的只是.map((不是函数。
有人能给我一些建议吗?
const Loader = () => (
<div class="divLoader">
<svg class="svgLoader" viewBox="0 0 100 100" width="10em" height="10em">
//Loader animation
</svg>
</div>
);
class Sonos extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: true,
data: [],
};
}
//API is called
componentDidMount() {
axios.get("API_KEY").then((res) => {
const data = res.data;
this.setState({ data: [], loading: false });
console.log(data);
});
}
render() {
return (
<div>
{this.state.loading ? <Loader /> : null}
<table border="1">
<parent>
<div className=" text-gray-700 items-center text-3xl ">
<div className="bg-gradient-to-r from-green-400 to-blue-500 min-w-full min-h-full">
<div className="text-4xl font-semibold">
{this.state.data.map((n) =>{
return (
<h1>pleasse</h1>
)
})}
</div>
</div>
</div>
</parent>
</table>
</div>
);
}
}
export { Sonos };
render(){
if(this.state.loading){
return <Loader />
}
// return table if loading is false
// handling error goes with the same logic
// table will only render if loading is false
return (
<table>
</table>
)
}