0

I just made an animated "loading.." using CSS but I want it to only exist when it's waiting for data to load/render. Any idea how to? I really don't know how as I'm so new to this.

This is my componentDidMount:

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

This is my render:

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 Answers 2

2

Maintain a state in your react component that is set to true whenever you give a call to load the data

Eg

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>
     )

}

See a demo snippet

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>

Sign up to request clarification or add additional context in comments.

15 Comments

It will output your data when you set the loading state to false as I did in my case. Your data will do at a place where I have return ( <div>Hello World</div> )
i did the same thing you put however instead of hello world it output my table but it wasn't working it still loading..
Are you doing this after your data has loaded this.setState({isLoading: false})
i put it under my ajax in componentdidmount
Can you add that part in your question
|
0

add loading state, and make it true, when data is loaded.

in render() function make add loading state code, using ? operator.

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

replace <YourComponent /> and <LoadingState /> with your component or needed jsx tags...

2 Comments

im confuse on your explanation
nvm, Shubham Khatri write same concept

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.