React-router v5 更新 URL 但不渲染嵌套组件

React-router v5 updates the URL but doesn't render nested component

我使用 reactreact-routerreact-reduxconnected-react-router 创建了一个应用程序,我有以下结构:

- app
  - admin
    - category
      - categoryList
      - categoryAdd

我的想法是执行以下应用程序流程:

/admin -> /admin/category -> /admin/category/add

但出于某种原因我看不到渲染的最后一个组件 (CategoryAdd)。

代码(沙盒link到底):

index.js

import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { Route, Switch, Link } from "react-router-dom"; 
import { ConnectedRouter } from "connected-react-router";
import configureStore, { history } from "./configureStore";

import AdminPage from "./AdminPage";
import Category from "./Category";

const store = configureStore(/* provide initial state if any */);

ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <>
        <Link to="/admin/category">Category</Link>
        <Switch>
          <Route exact path="/admin" component={AdminPage} />
          <Route exact path="/admin/category" component={Category} />
        </Switch>
      </>
    </ConnectedRouter>
  </Provider>,
  document.getElementById("root")
);

AdminPage.js

import React from "react";
import { Link } from "react-router-dom";

export default function AdminPage() {
  return (
    <div style={{ border: "1px solid black", margin: 20 }}>
      <div>AdminPage</div>
      <Link to="/admin/category">Catgory page link</Link>
    </div>
  );
}

Category.js

import React from "react";
import { Route, Switch } from "react-router-dom";
import { withRouter } from "react-router-dom";
import CategoryList from "./CategoryList";
import CategoryAdd from "./CategoryAdd";

function Category() {
  return (
    <Switch>
      <Route path={`/admin/category`} render={() => <CategoryList />} />
      <Route path={`/admin/category/add`} render={() => <CategoryAdd />} />
    </Switch>
  );
}

export default withRouter(Category);

CategoryList.js

import React from "react";
import { Link } from "react-router-dom";

export default function CategoryList() {
  return (
    <div style={{ border: "1px solid black", margin: 20 }}>
      CategoryTable <Link to="/admin/category/add">Add</Link>
    </div>
  );
}

CategoryAdd.js

export default function Add() {
  return <div style={{ border: "1px solid black", margin: 20 }}>Add Page</div>;
}

这里是沙箱:https://codesandbox.io/embed/dark-star-3fhs0

您使用的 Switch 仅匹配一条路线,因此您在 index.jsCategory 组件中的第二条路线将永远不会呈现。

1) 在index.js

中删除Switch组件和exact路由道具
ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <>
        <Link to="/admin/category">Category</Link>
        <Route path="/admin" component={AdminPage} />
        <Route path="/admin/category" component={Category} />
      </>
    </ConnectedRouter>
  </Provider>,
  document.getElementById("root")
);

2) 删除 Category

中的 Switch
function Category() {
  return (
    <>
      <Route path={`/admin/category`} render={() => <CategoryList />} />
      <Route path={`/admin/category/add`} render={() => <CategoryAdd />} />
    </>
  );
}

这在您的代码沙箱中有效,但您的 CategoryAdd 组件已损坏,因为您没有导入 React。所以在第一行添加它:

import React from 'react';

结果:

那是因为您的 /admin/category 路线上的 [​​=11=] 关键字:

<Route exact path="/admin/category" component={Category} />

这只匹配 /admin/category,而不匹配 /admin/category/add

不幸的是,删除 exact 只会解决您的第一个问题。

另一个是你对Switch的使用。您应该删除 Index.js 中的 Switch,因为一旦您匹配了 /admin/category 路由,它就不会匹配 Category-Component.[=20= 中的任何路由]