【发布时间】:2020-06-14 01:10:52
【问题描述】:
使用 React Route,但我遇到了这个问题:在某些情况下,我需要应用程序加载与实际不同的 url。这是我所做的:
class Application extends React.Component {
render() {
const { history } = this.props;
let loadExact = true;
if (my_condition === true) {
loadExact = false;
}
return (
<Dashboard history={history}>
<Switch>
{
loadExact ? <Route exact path="/app" component={MainPage} /> :
(
<Redirect to={{
pathname: "/app/theotherpage",
state: {from: history}
}}
/>
)
}
<Route path="/app/theotherpage" component={TheOtherPage} />
<Route component={NotFound} />
</Switch>
</Dashboard>
);
}
}
我需要的是如果loadExact 是false 然后重定向到/app/theotherpage 并因此加载它而不是/app。这样它就进入了一个完全空白的页面,它告诉我:
无法对未安装的组件执行 React 状态更新。这是一个空操作,但它表明您的应用程序中存在内存泄漏。要修复,请在 componentWillUnmount 方法中取消所有订阅和异步任务。
我该如何解决这个问题?
【问题讨论】:
-
尝试使用
history.push()。它还说您没有进行清理
标签: reactjs redirect react-router