如何修复受污染的状态?
好吧,我又回来了,还有另一个问题,是的!
这个问题比编码更多的是思考,但我将包括一小部分代码 相关。
有什么问题?
成像拥有像“马尔特戈”这样的网站,该网站获取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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论