如何在React上显示错误消息?

发布于 2025-02-11 10:04:22 字数 1306 浏览 0 评论 0原文

以下代码用作Excel文件的用户的输入接收器,如果Excel文件的格式正确,则将其带到Mainpage。我想显示错误消息(弹出甚至警报),我似乎无法弄清楚如何做。

我尝试了工具提示,但它与该问题无关,但我尝试了警报,但这不起作用,没有说出它的外观。

function App() {

  const [fileData, setFileData] = useState<any>();

  const handleFileData = (data: any) => {
    for (let index = 0; index < data[0].length; index++) {
      if (data[0][index] != null) {
        if (data[0][index].includes("name" && "id")) {
          setFileData(data);
        }
      }
    }
  }
  const isData = () => {
    if (fileData !== undefined) {
      return <MainPage Data={fileData}></MainPage>;
    }
    return HOMEPAGE

  }

  const HOMEPAGE = (
    <div className="App" style={{ "overflowX": "hidden", }}>
      <h1 style={{ "marginBottom": "50px", "marginTop": "25px" }}>NEXUM Interface</h1>
      <InputPart handleFileData={handleFileData}></InputPart>
    </div>
  );

  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={HOMEPAGE}></Route>
        <Route path="/main" element={isData()}></Route>
      </Routes>
    </BrowserRouter>
  );
}

export default App;

如果您对我有建议或可以格式化代码在发生错误时显示错误消息(查看ISDATA,目前我要返回主页,我想用错误消息返回主页),这将有助于很多。

The following code Is used as an input receiver from the user of an excel file, and if the excel file is in the right format, it will take it to MAINPAGE.

but, here's the trick, When inputting a false formatted excel file (xlsx) I want to display an error message (Pop up Or even an alert), And i can't seem to figure out how to do it.

I tried tooltip but its unrelated to the issue, And I tried an alert but that didn't work, not speaking about the looks of it which was extremely bad looking.:

function App() {

  const [fileData, setFileData] = useState<any>();

  const handleFileData = (data: any) => {
    for (let index = 0; index < data[0].length; index++) {
      if (data[0][index] != null) {
        if (data[0][index].includes("name" && "id")) {
          setFileData(data);
        }
      }
    }
  }
  const isData = () => {
    if (fileData !== undefined) {
      return <MainPage Data={fileData}></MainPage>;
    }
    return HOMEPAGE

  }

  const HOMEPAGE = (
    <div className="App" style={{ "overflowX": "hidden", }}>
      <h1 style={{ "marginBottom": "50px", "marginTop": "25px" }}>NEXUM Interface</h1>
      <InputPart handleFileData={handleFileData}></InputPart>
    </div>
  );

  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={HOMEPAGE}></Route>
        <Route path="/main" element={isData()}></Route>
      </Routes>
    </BrowserRouter>
  );
}

export default App;

If u have an advice for me or can format the code for it to display an error message in case of an error (Look at isData, currently I'm returning the HOMEPAGE, I want to return the HOMEPAGE with an error message) It will help alot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文