使用反应路由器使用侧边栏导航动态渲染组件的更简单方法

问题描述 投票:1回答:2

目前正在通过本教程创建一个带有反应路由器https://reacttraining.com/react-router/web/example/sidebar的侧边栏导航系统

我计划有多条路线,这意味着我必须继续导入路线并将它们添加到routes阵列。有动态加载它们的智能/正确方法吗?

我的所有组件都将在我的/Views文件夹中。

App.js

import React, { Component } from 'react';
import SideBar from './components/SideBar/SideBar';
import MainContent from './components/MainContent/MainContent';
import { BrowserRouter as Router,
} from 'react-router-dom';


// Import all components here
import Button from './components/Views/Button/Button';
import Color from './components/Views/Color/Color';
import Card from './components/Views/Card/Card';
import Filter from './components/Views/Filter/Filter';

const routes = [
  {
    path: "/",
    name: 'home',
    exact: true,
    main: () => <h2>Home</h2>
  },
  {
    path: "/button",
    name: 'Button',
    main: () =>  <Button />
  },
  {
    path: "/color",
    name: 'Color',
    main: () =>  <Color />
  },
  {
    path: "/card",
    name: 'Card',
    main: () =>  <Card />
  },
  {
    path: "/filter",
    name: 'Filter',
    main: () =>  <Filter />
  },
];

class App extends Component {
  render() {
    return (
      <Router>
        <div className="ds-container">
          <SideBar routes={routes} />
          <MainContent routes={routes} />
        </div>
      </Router>
    );
  }
}

export default App;
javascript reactjs react-router jsx react-router-dom
2个回答
1
投票

既然,你正在使用内部使用webpack的create-react-app,你可以查看require-context。这将帮助您动态导入与特定正则表达式匹配的文件夹中的所有文件。 (例如:以.jsx / .js结尾)

但是,我建议你反对它:

  1. 在任何时候你都不知道你目前正在迎合什么路线。
  2. 它可能会降低您的代码可读性。
  3. 您可能还必须导出组件的映射(路径中的path)以及组件本身。

为了避免所有这些,您可以在index.js组件中创建一个Views文件,该文件需要您创建的任何新路由组件并返回您在App.js文件中形成的最终数组。

基本上,/ Views / index.js:

// Import all components here
import Button from './components/Views/Button/Button';
import Color from './components/Views/Color/Color';
import Card from './components/Views/Card/Card';
import Filter from './components/Views/Filter/Filter';

const routes = [
  {
    path: "/",
    name: 'home',
    exact: true,
    main: () => <h2>Home</h2>
  },
  {
    path: "/button",
    name: 'Button',
    main: () =>  <Button />
  },
  {
    path: "/color",
    name: 'Color',
    main: () =>  <Color />
  },
  {
    path: "/card",
    name: 'Card',
    main: () =>  <Card />
  },
  {
    path: "/filter",
    name: 'Filter',
    main: () =>  <Filter />
  },
  // add new routes here
];

export default routes;

在SideBar.js中:

import routes from 'path/to/views/Views';

//rest of your code to render the routes.

这样,您将清理App.js中的代码,并且还能够有效地分离各个组件的关注点。

我希望这是有道理的 :)


0
投票

根据当前位置,有多种方法可以选择主要组件。但是所有这些都需要列出所有可能的路线并导入相应的组件。您可以使用dynamic importsReact.lazy来更轻松地进行代码拆分。

但是您可以避免侧边栏的额外配置。侧边栏可以从全局状态获取配置,您的主要组件可以在mount(componentDidMountuseEffect(() => {...}, []))上更新该状态:

const SideBarContext = React.createContext(() => {});

function useSidebar(name) {
  const setSidebarName = useContext(SideBarContext);
  useEffect(() => {
    setSidebarName(name);
    return () => setSidebarName(null);
  }, []);
}

function Home() {
  useSidebar('home');
  return <h2>Home</h2>;
}

function Button() {
  useSidebar('Button');
  return <button>press me</button>;
}

function App() {
  const [name, setName] = useState(null);

  return (
      <Router>
        <div className="ds-container">
          <SideBar name={name} />
          <SideBarContext.Provider value={setName}>
            <Route path="/" exact component={Home}/>
            <Route path="/button" component={Button}/>
          </SideBarContext.Provider>
        </div>
      </Router>
  );
}

因此,每个组件都会关注侧边栏的选项,您只需要导入它们并添加Routes。

© www.soinside.com 2019 - 2024. All rights reserved.