【问题标题】:How to solve React-Router-dom contradicting with context API state如何解决与上下文 API 状态相矛盾的 React-Router-dom
【发布时间】: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


    【解决方案1】:

    这样使用!! &lt;Route path='*' exact component={NotFound} /&gt;

    【讨论】:

    • 请在您的答案中添加一些解释,以便其他人可以从中学习
    • 我已经尝试过了……结果相同……作为重定向……
    猜你喜欢
    • 2018-07-22
    • 2023-02-24
    • 2017-09-30
    • 1970-01-01
    • 2021-03-31
    • 2015-06-06
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多