第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用React Router通過邊欄導航動態(tài)呈現(xiàn)組件的更簡單方法

使用React Router通過邊欄導航動態(tài)呈現(xiàn)組件的更簡單方法

守著星空守著你 2021-04-07 13:09:51
當前正在本教程中學習如何使用React Router https://reacttraining.com/react-router/web/example/sidebar創(chuàng)建側(cè)邊欄導航系統(tǒng)我計劃有多個路由,所以這意味著我必須繼續(xù)導入路由并將其添加到routes陣列中。是否有一種聰明/正確的方式來動態(tài)加載它們?我所有的組件都在我的/Views文件夾中。App.jsimport 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 hereimport 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;
查看完整描述

2 回答

?
慕工程0101907

TA貢獻1887條經(jīng)驗 獲得超5個贊

由于您使用的是內(nèi)部使用webpack的create-react-app,因此可以查看require-context。這將幫助您動態(tài)導入與某個正則表達式匹配的文件夾中的所有文件。(例如:以.jsx / .js結(jié)尾)

但是,我建議您反對:

  1. 您永遠不會知道您目前正在適應的路線。

  2. 這可能會降低代碼的可讀性。

  3. 您可能還必須將組件的映射(path在Route中)連同組件本身一起導出。

為了避免所有這些情況,您可以index.jsViews組件中簡單地創(chuàng)建一個文件,該文件將需要您創(chuàng)建的任何新的路由組件,并返回在App.js文件中形成的最終數(shù)組。

因此,本質(zhì)上是/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中的代碼,并且還可以有效地分離各個組件的問題。


查看完整回答
反對 回復 2021-04-08
  • 2 回答
  • 0 關(guān)注
  • 369 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號