【发布时间】:2021-10-16 00:37:49
【问题描述】:
我有以下从 App.js 获取的返回函数,为了清楚起见,主要问题是一旦我点击未定义的路由而不是重定向到 404,它会显示白屏,重定向根本不起作用,甚至如果我使用不同的方法,例如在切换之前将 404 组件放在底部,它仍然无法正常工作,我一直在尝试但完全没有运气,导致这种情况的是状态,正如您在代码中看到的我如果我确实将所有提供程序设置为整个应用程序,则为某些组件提供状态完全没有问题,但我不希望这样,我有 3 个状态 一个全局状态包含四个组件,两个状态包含两个路由,我什至尝试根据一个关于类似问题的答案创建上下文路由,但仍然没有运气,我认为状态以某种方式阻塞了流程,出于某种原因任何想法围绕这个?
return (
<Router>
<Fragment>
<Switch>
<Route path='/' exact component={Home} />
<Route path='/signin' exact component={SignIn} />
<Route path='/restpassword' exact component={RestPassword} />
<Route path='/signup' exact component={SignUp} />
<Route path='/404' exact component={NotFound} />
{/* <ContextRoute exact path='/dashboard' contextComponent={ModalState} component={Dashboard} /> */}
<GlobalState>
<Route path='/dashboard' exact component={Dashboard} />
<FirstState>
<Route path='/inofrmationA' exact component={inofrmationA} />
</FirstState>
<SecondState>
<Route path='/inofrmationB' exact component={informationB} />
</SecondState>
<Route path='/settings' exact component={Settings} />
</GlobalState>
// If nothing the trigger redirection to 404 route.
<Redirect to='/404' />
</Switch>
</Fragment>
</Router>
);
【问题讨论】:
标签: react-router-dom react-context