React : How to Show Message if there is no records

12,574

Solution 1

You can check when you get the data back and set an error if no data:

  getData(){
    const {Item,skip}=this.state;
    axios.get(`http://localhost:8001/parties?filter[limit]=${Item}&&filter[skip]=${skip}`)
    .then(response=>{
      console.log(response.data);
      if (!response.data.length) {
        this.setState({noData: true}) 
      } else {
        this.setState({
          data:response.data, noData: false
        })
      }
    })
  }

Then in your render function:

render() {
  if (this.state.noData) {
    return <p>No Data was returned!</p>;
  }
  ...

Solution 2

You could check for the data before render the component and return another component if you don't have any. Using expressions such This example ‘’’ { doIHaveData ? < Component1 /> : < Component2 />} ‘’’

Where Component1 has your functionality and Component2 return a message or a spinner loader whatever you want .

I hope it helps!

When you are checking for the empty array you could check for array type and length. I would personally do something like

 {Array.isArray(array) && array.length === 0 ? <component1/> : <component2/>} 

Solution 3

You can make use of conditional rendering!

render(){
const filteredItems = this.getDataItems(this.state.filtered);
const dataItems = this.getDataItems(this.state.data);

if(dataItems){
return(
 <div>Your Message</div>
)
}
else{
//Your normal code

}
}
Share:
12,574

Related videos on Youtube

Jon
Author by

Jon

Updated on June 04, 2022

Comments

  • Jon
    Jon almost 2 years

    I am working on project in ReactJS, I am fetching data from server through API. I did some search filtration, I want to display message if there is no records available? I am beginner to ReactJS and don't have much knowledge related to ReactJS.

    Code:

            class Example extends React.Component {
      constructor(props) {
        super(props);
    
        this.state = {
          Item: 5,
          skip: 0
        }
    
        this.handleClick = this.handleClick.bind(this);
      }
    
      urlParams() {
        return `http://localhost:3001/meetups?filter[limit]=${(this.state.Item)}&&filter[skip]=${this.state.skip}`
      }
    
      handleClick() {
        this.setState({skip: this.state.skip + 1})
      }
    
      render() {
        return (
          <div>
            <a href={this.urlParams()}>Example link</a>
            <pre>{this.urlParams()}</pre>
            <button onClick={this.handleClick}>Change link</button>
          </div>
        )
      }
    }
    
    
    ReactDOM.render(<Example/>, document.querySelector('div#my-example' ))
    
  • Jon
    Jon about 5 years
    Actually , It not working . I want that if there is no data in array I want to show message .
  • Jon
    Jon about 5 years
    I want to set error on search filter , if there is no data in array
  • jsdeveloper
    jsdeveloper about 5 years
    Ok in render() just check if this.state.filteredData has any results. If not, add an error message in the render output.
  • Jon
    Jon about 5 years
    Tried but still not working ! Can you please edit your answer and write exact code ? Thanks
  • jsdeveloper
    jsdeveloper about 5 years
    Jon - I think you need to take the time to try to understand the technologies you are working with. What will you learn if others write the solution every time?