【发布时间】:2021-09-25 17:36:28
【问题描述】:
我正在尝试在页面之间导航而不会丢失页面数据。例如,我有一个包含许多输入字段的页面,如果用户填写了所有这些字段并尝试移动到另一个页面并返回到第一个页面,那么所有输入都消失了。我正在使用 react-router-dom 但没有找到防止这种情况发生的方法。
到目前为止我做了什么:
import { Route, Switch, HashRouter as Router } from "react-router-dom";
<Router>
<Switch>
<Route path="/home" exact component={Home} />
<Route path="/hello-world" exact component={HellWorld} />
</Switch>
</Router>
主页组件:
navigateToHelloWorld= () => {
this.props.history.push('/hello-world')
};
Hello World 组件:
this.props.history.goBack();
【问题讨论】:
-
我不知道这样的普遍性可以得到支持。我只会将所有状态变量值存储在 localStorage 中,并在组件呈现上存在值时从那里恢复(使用
useState时,然后作为默认值)。
标签: reactjs react-router react-router-dom