Search code examples
reactjsapiaxioserror-messaging

How can show "no data available" message if there will be not data available in react js axios?


I want to display a message "no data available " if there will be no data after fetching from API in react js I don't know how to do you can look my code and guide me

{this.state.movies1.map(c => (
              <tbody key={c.pk}>
                <tr>
                  <th scope="row">{c.pk}</th>
                  <td>
                    <a
                      href="/overview/4343"
                      style={{ color: "#13B760" }}
                      class="font-weight-bold"
                    >
                      {c.user}
                    </a>
                  </td>
                  <td>{c.total_milage ? `${c.total_milage}` : 0} </td>
                  <td>{c.total_movingtime ? `${c.total_movingtime}` : 0} </td>
                  <td>
                    {c.total_averagespeed ? `${c.total_averagespeed}` : 0} 
                  </td>
                  <td>{c.total_letter ? `${c.total_letter}` : 0} </td>
                  <td>
                    {c.total_ship_weight ? `${c.total_ship_weight}` : 0}
                  </td>
                  <td>{c.total_pack ? `${c.total_pack}` : 0}</td>
                  <td>{c.total_kg ? `${c.total_kg}` : 0} </td>
                  <td>{c.total_co2_save ? `${c.total_co2_save}` : 0}  </td>
                  <td>{c.total_boxes ? `${c.total_boxes}` : 0}</td>
                  <td>{c.total_user ? `${c.total_user}` : 0}</td>
                </tr>
              </tbody>
            ))}

If the data will be not available in this table it should display a message that "no data available " so help me thanks


Solution

  • Simply check your object length or null/undefined before map with help of conditional operator.

    {this.state.movies1.length > 0 ? this.state.movies1.map(c => (
                      <tbody key={c.pk}>
                        <tr>
                          <th scope="row">{c.pk}</th>
                          <td>
                            <a
                              href="/overview/4343"
                              style={{ color: "#13B760" }}
                              class="font-weight-bold"
                            >
                              {c.user}
                            </a>
                          </td>
                          <td>{c.total_milage ? `${c.total_milage}` : 0} </td>
                          <td>{c.total_movingtime ? `${c.total_movingtime}` : 0} </td>
                          <td>
                            {c.total_averagespeed ? `${c.total_averagespeed}` : 0} 
                          </td>
                          <td>{c.total_letter ? `${c.total_letter}` : 0} </td>
                          <td>
                            {c.total_ship_weight ? `${c.total_ship_weight}` : 0}
                          </td>
                          <td>{c.total_pack ? `${c.total_pack}` : 0}</td>
                          <td>{c.total_kg ? `${c.total_kg}` : 0} </td>
                          <td>{c.total_co2_save ? `${c.total_co2_save}` : 0}  </td>
                          <td>{c.total_boxes ? `${c.total_boxes}` : 0}</td>
                          <td>{c.total_user ? `${c.total_user}` : 0}</td>
                        </tr>
                      </tbody>
                    )): <div>No Data avaliable</div>>}