简体   繁体   English

如果是“null”或“undefined”,则不要在 React js 中显示表格

[英]In case of "null" or "undefined" dont display the table in react js

here is the code which displaying the all the data i have to put check if body contains nulll then dont display table.Currently it displaying the empty table.but that empty table shouldnt be visible.any help would be apprecited这是显示我必须输入的所有数据的代码,检查 body 是否包含 null 然后不显示表。目前它显示空表。但是那个空表不应该是可见的。任何帮助将不胜感激

  {logs &&
    logs.map((log) => {
      if (
        log.body === "null" ||
        log.body === "undefined" ||
        log.body === ""
      ) {
        console.log("empty body");
      } else {
        return (
          <Table striped bordered hover>
            <thead>
              <tr>
                <th>Parameter</th>
                <th>
                  Value <CButton onClick={handleShow}>View</CButton>
                </th>
              </tr>
            </thead>
            <tbody>
              {log.body &&
                Object.keys(log.body).map((key) => ( 
                  <tr key={key}>
                    <td>{key}</td>
                    <td>{JSON.stringify(log.body[key])}</td>
                  </tr>
                ))}
            </tbody>
          </Table>
        );
      }
    })}

And here is the API.这是 API。

[
    {
        "id": 1,
        "body": {
            "buy_WBNB": 0.1231,
            "sell_WBNB": 0.1541, 
            "expected_profit": -42.27542384981283,  
        },
        "response": "CONNECTION ERROR: The connection got closed with the close code `4040`",
        "created_at": "Feb 28, 2022 11:57 AM"
    },
    {
        "id": 2011,
        "body": null,
        "response": "connection not open on send()",
        "created_at": "Feb 28, 2022 5:49 PM"
    }, 
]

check first log.body exist首先检查 log.body 是否存在

{logs &&
    logs.map((log) =>
    log.body && Object.keys(log.body).map((key) => (
        <tr key={key}>
          <td>{key}</td>
          <td>{JSON.stringify(log.body[key])}</td>
        </tr>
      ))
    )}

output will look like: output 看起来像: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM