我正在使用Axios获取数据,并希望加载动画更长时间(出于个人原因).
componentDidMount(){
var that = this;
axios.get('api.something.io.json', {
params: data
})
.then(function (response) {
console.log(response);
that.setState({
axiosResponse: response.data,
isLoading: false
})
})
.catch(function (error) {
console.log(error);
});
};
...
render() {
if(this.state.isLoading){
return <div>Waiting...</div>
}
return (
<div className="App">
<h1>{this.state.axiosResponse.awesomeTitle}</h1>
</div>
);
Run Code Online (Sandbox Code Playgroud)
现在Waiting...在主要组件渲染之前显示一瞬间.我想延长"Waiting..."一段时间,大约2-3秒.我尝试使用的setTimeout上componentDidMount,但它似乎没有工作.
componentDidMount(){
setTimeout(this.setState({isLoading: 1}), 3000);
//do axios call here
}
Run Code Online (Sandbox Code Playgroud)
我也尝试在.then调用中存根setTimeout :
componentDidMount(){
var that = this;
axios.get('https://api.rss2json.com/v1/api.json', {
params: data
})
.then(function (response) {
console.log(response);
setTimeout(
that.setState({
axiosResponse: response.data,
isLoading: false
})
, 3000);
})
...
Run Code Online (Sandbox Code Playgroud)
没有用.如何延长isLoading3秒?
setTimeout 接受函数作为第一个参数:
setTimeout(function() {
that.setState({
axiosResponse: response.data,
isLoading: false
})
}, 3000);
Run Code Online (Sandbox Code Playgroud)
并不是 :
setTimeout(
that.setState({
axiosResponse: response.data,
isLoading: false
}), 3000);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2383 次 |
| 最近记录: |