【问题标题】:Redirect in React Router 5 is successfully redirecting, but not rendering the child componentReact Router 5 中的 Redirect 成功重定向,但不渲染子组件
【发布时间】:2020-05-27 08:09:58
【问题描述】:

我已经更改了我的应用程序中的路线,如果任何用户已将旧路线的网址添加为书签,我已经添加了一些重定向到新路线。他们中的大多数都工作正常,但是这个不是 -

App.tsx

import { Router } from 'react-router-dom';
import Routes from './Routes';
import history from './history';

const App: FunctionComponent = () => (
  <Router history={history}>
    <Routes />
  </Router>
);

export default App;

RouteSwitch.txs

import React, { FunctionComponent } from 'react';
import { Switch, Route, Redirect } from 'react-router-dom';

const RouteSwitch: FunctionComponent = () => {
  return (
    <Switch>
      <Redirect exact from="/documents" to="/documents/list" />
      <Route exact path="/documents/list">
        <DocumentsContainer />
      </Route>
    </Switch>
  );
};

export default RouteSwitch;

从/documents 到/documents/list 的重定向有效,但是DocumentsContainer 没有被渲染。如果我直接请求/documents/list,那么它会很好。就好像&lt;Switch&gt; 找到了它的第一个匹配项(重定向),然后决定它的工作已经完成。我尝试将 push 属性添加到重定向,但没有任何区别。

我的示例与 React 培训网站上给出的示例非常相似 - https://reacttraining.com/react-router/web/api/Redirect/from-string

想法?

【问题讨论】:

  • 我们如何在没有代码的情况下帮助您? .. 这个链接对我们很有用:stackoverflow.com/help/how-to-ask.
  • 抱歉,这是我向 SO 提出的第一个问题,发现在标签字段中按 Enter 会导致问题过早提交
  • &lt;Route exact path="/documents/list" component={DocumentsContainer} /&gt;。这段代码能帮到你吗?
  • 很好的建议,但是这给出了相同的行为
  • 我为重定向尝试了这种替代方法,但它的行为仍然相同 - &lt;Route exact path="/documents" render={() =&gt; &lt;Redirect to="/documents/list" /&gt;} /&gt;

标签: reactjs react-router


【解决方案1】:

问题已解决 - DocumentsContainer 组件实际上 呈现,但由于使用重定向如何导致加载程序计数增加一的怪癖而没有显示任何内容(但不递减)导致加载器计数不返回 0 以允许从 Redux 加载文档数据并呈现内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-22
    • 2020-12-22
    • 2016-11-05
    • 2022-06-17
    • 2018-09-20
    • 1970-01-01
    相关资源
    最近更新 更多