gpt4 book ai didi

javascript - 如何在等待渲染 Reactjs 时进行加载

转载 作者:行者123 更新时间:2023-12-01 03:36:34 31 4
gpt4 key购买 nike

我刚刚使用CSS制作了一个动画“正在加载..”,但我希望它仅在等待数据加载/渲染时才存在..你知道我真的不知道我对此如此陌生吗?...

这是我的组件挂载

componentDidMount() {
$.ajax({
url:'http://localhost:5118/api/employeedetails/getemployeedetails/',
success:(data)=>{

this.setState({
jsonReturnedValue:data
})
}
})
this.setState({isLoading: false})
}

这是我的渲染

renderItem(d, i) {
return <tr key={i} >
<td> {d.Employee_ID} </td>
<td>{d.Employee_Name}</td>
<td>{d.Address }</td>
<td><center><button className ="btn btn-info" onClick={this.handleOnclick.bind(this, d.Employee_ID, d.Employee_Name, d.Address , d.Department , i)} data-toggle="modal" data-target="#UpdateEmployee">Edit</button></center></td>
<td><center><button className ='btn btn-danger' onClick={this.handleOnclick2.bind (this,d.Employee_ID,d.Employee_Name,i)} data-toggle="modal" data-target="#DeleteEmployee"> Delete</button></center></td>
</tr>
}

render() {
if(this.state.isLoading) {
return <span className="Loader">
<div className="Loader-indicator" >
<h1>
<span>Loading</span>
<span className="Loader-ellipsis" >
<span className="Loader-ellipsisDot">.</span>
<span className="Loader-ellipsisDot">.</span>
<span className="Loader-ellipsisDot">.</span>
</span>
</h1>
</div>
</span>
}

let {jsonReturnedValue} = this.state;
const isEnabled = this.canBeSubmitted();


return(
<div>
<div>

<div className="container">
<h1> Listof Employees </h1>
<button className ='btn btn-warning right ' data-toggle="modal" data-target="#AddEmployee"> Add an Employee</button>
<table className= "table table-bordered" id="result">
<tbody>
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Update</th>
<th>Delete</th>
</tr>
{jsonReturnedValue.map((d,i) => this.renderItem(d,i))}
</tbody>

</table>
</div>

最佳答案

在您的 react 组件中维护一个状态,每当您调用加载数据时该状态都设置为 true

例如

class App extends React.Component {
constructor(){
super();
this.state = {isLoading: true}
}


componentDidMount() {


$.ajax({
url:'http://localhost:5118/api/employeedetails/getemployeedetails/',
success:(data)=>{
this.setState({
jsonReturnedValue:data, isLoading: false
})
}
})

}

render() {
if(this.state.isLoading) {
return <span className="Loader">
<div className="Loader-indicator" >
<h1>
<span>Loading</span>
<span className="Loader-ellipsis" >
<span className="Loader-ellipsisDot">.</span>
<span className="Loader-ellipsisDot">.</span>
<span className="Loader-ellipsisDot">.</span>
</span>
</h1>
</div>
</span>
}
return (
<div>Hello World</div>
)

}

查看演示片段

class App extends React.Component {
constructor(){
super();
this.state = {isLoading: true}
}


componentDidMount() {

setTimeout(() => {this.setState({isLoading: false})}, 3000)

}

render() {
if(this.state.isLoading) {
return <span className="Loader">
<div className="Loader-indicator" >
<h1>
<span>Loading</span>
<span className="Loader-ellipsis" >
<span className="Loader-ellipsisDot">.</span>
<span className="Loader-ellipsisDot">.</span>
<span className="Loader-ellipsisDot">.</span>
</span>
</h1>
</div>
</span>
}
return (
<div>Hello World</div>
)

}
}
ReactDOM.render(<App/>, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>

关于javascript - 如何在等待渲染 Reactjs 时进行加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44234398/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com