【发布时间】:2021-09-27 01:52:16
【问题描述】:
我只是在检查我在 redux 商店中是否有语言。如果有,我会将其保存在我的localStorage 中,如果没有,我想导航到组件调用MultiLanguage。
我使用history 使用以下方法,但它显示 mw 错误。显示undefined 的推送是什么?对此有什么解决方案?
const history = useHistory();
const language = store.getState().languages.selectedLangauge.language;
console.log("language", language);
if (language) {
localStorage.setItem("language", language);
} else {
history.push("/multilanguage");
}
return (
<div className="font-metropolis_regular">
<Router>
<Route path="/" exact component={Selection} />
<Route path="/login" component={Login} />
<Route path="/home" component={Homepage} />
<Route path="/account" component={Account} />
<Route path="/multilanguage" component={MultiLanguage} />
</Router>
</div>
);
【问题讨论】:
-
历史未定义。
-
一个错误是使用 switch 作为所有路由的包装器reactrouter.com/web/api/Switch
-
@AvaniBataviya 现在,我添加了 Switch,但仍然出现错误。
标签: javascript reactjs react-router-dom