如何在等待渲染Reactjs时加载

时间:2017-05-29 03:59:27

标签: javascript jquery ajax reactjs web

我刚做了一个动画片#34; loading .."使用css但我希望它只在等待数据加载/渲染时才存在..任何想法我怎么真的不知道这对我来说有多新呢?...

这是我的componentdidmount

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>

2 个答案:

答案 0 :(得分:2)

每当您调用加载数据时,保持您的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>

答案 1 :(得分:0)

添加loading状态,并在加载数据时将其设为true。

render()函数中的

使用?运算符生成加载状态代码。

{this.state.loading ? (<YourComponent />) : (<LoadingState />)}

<YourComponent /><LoadingState />替换为您的组件或所需的jsx标签......