React : How to Show Message if there is no records

前端 未结 5 1075
耶瑟儿~
耶瑟儿~ 2021-01-16 18:35

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 a

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-16 19:30

    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 

    No Data was returned!

    ; } ...

提交回复
热议问题