如何修复受污染的状态?

发布于 2025-02-12 16:51:09 字数 1003 浏览 1 评论 0原文

好吧,我又回来了,还有另一个问题,是的!

这个问题比编码更多的是思考,但我将包括一小部分代码 相关。

有什么问题?
成像拥有像“马尔特戈”这样的网站,该网站获取Excel文件并显示连接,现在成像 输入一个不应该工作的糟糕文件,会发生什么?错误,然后您可以重试并输入另一个文件。

但是!
由于涉及“ Usestate”,因此,当您返回时,“ Usestate” dost重置,它会保持污染,现在您在代码中遇到了问题。

现在我的问题是,进入组件后如何清除状态?

一点点代码:

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

const handleFileData = (data: Array<any>) => {
          setFileData(data);}

  const isData = () => {
    if (fileData !== undefined) { return <MainPage Data={fileData}></MainPage> }
    else { return (HOMEPAGEerrorversion) }
  };
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={HOMEPAGE}></Route>
        <Route path="/main" element={isData()}></Route>
      </Routes>
    </BrowserRouter>
  );

PS。在“ HomePageErrorversion”中,我从材料UI中有一种模态,它在关闭时,它返回到第一页,称为首页。

Well, I'm back again, With another problem, Yay!

This problem is more thinking than coding, But I will include a small part of the code that might
be related.

What's the problem?

imaging having a website like 'MALTEGO' that takes an excel file and shows connection, Now imaging
entering a bad file, that doesn't supposed to work, What will happen? An error, And then u can Retry and enter another file.




But!

Because There is a 'useState' involved, when you go back, the 'useState' dost reset, it stays contaminated and now you have problems in the code.

Now my question is this, How can I clear the state, Upon entry to a component?

A Bit of the code:

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

const handleFileData = (data: Array<any>) => {
          setFileData(data);}

  const isData = () => {
    if (fileData !== undefined) { return <MainPage Data={fileData}></MainPage> }
    else { return (HOMEPAGEerrorversion) }
  };
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={HOMEPAGE}></Route>
        <Route path="/main" element={isData()}></Route>
      </Routes>
    </BrowserRouter>
  );

Ps. In the 'homepageErrorVersion' I have a modal from the Material UI that upon closing, it goes back to the first page, Known as HOMEPAGE.

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

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

发布评论

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