如何将React Router拆分为多个文件

时间:2018-09-19 17:09:03

标签: javascript reactjs react-router react-router-v4

我的路线文件变得非常混乱,因此我决定将它们分成单独的文件。

我的问题是,如果我使用了2个单独的文件,则第一个include之后的任何一个都不会呈现:

const routes = (
  <div>
    <Switch>
      <Route exact path="/" component={Home} />
      {Registration} //Does get rendered

      //Everything below this does not get a route
      {Faq}
      <Route path="/login" component={login} />
      <Route component={NoMatch} />
    </Switch>
  </div>
);

如果我将Faq切换为registration,则所有常见问题解答路由都将起作用。

RegistrationRoutes.js

import Introduction from '../containers/Registration/Introduction';
import Login from '../containers/Login';

const Routes = (
  <Switch>
    <Route path="/login" component={Login} key="login" />,
    <Route path="/registration/introduction" component={Introduction} key="registration-intro" />
  </Switch>
);
export default Routes;

FaqRoutes.js

import Faq from '../containers/Faq';
import faqJson from '../json_content/faq/faq';
import FaqCategory from '../containers/Faq/faqCategory';

const Routes = (
  <Switch>
    <Route path="/faq/:category" component={FaqCategory} key="faqCat" />
    <Route path="/faq" render={props => <Faq data={faqJson} />} key="faq" />
  </Switch>
);
export default Routes;

3 个答案:

答案 0 :(得分:1)

也许您可以将它们移动到配置文件并从那里加载它们。

App.tsx

import routes from "./routes";
const App: React.FC = () => {
  return (
      <BrowserRouter>
        <div>
          <Switch>
            {routes.data.map((entry) => {return (<Route {...entry}/>)})}
          </Switch>
        </div>
      </BrowserRouter>
  );
};

export default App;

router.ts

const routes = {data: [
        {
            key: "one",
            path: "/three"

        },
        {
            key: "two",
            path: "/two"
        }
    ]
}

export default routes;

这将使您的代码保持简单

答案 1 :(得分:0)

您的代码将被翻译为类似的内容,

const routes = (
  <div>
    <Switch>
      <Route exact path="/" component={Home} />
      <Switch>
         <Route path="/login" component={Login} key="login" />,
         <Route path="/registration/introduction" 
             component={Introduction} key="registration-intro" />
      </Switch>

      //Everything below this does not get a route
      {Faq}
      <Route path="/login" component={login} />
      <Route component={NoMatch} />
    </Switch>
  </div>
);

react-router-domReact-Router v4实现路由的错误方法。

有关正确的实现方式,您可以查看this example

index.js

import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, Switch, Route, Link } from "react-router-dom";

import LandingPage from "../Registration";

const Home = () => {
  return <div>
  Home Component
   <Link to="/auth/login">Login</Link>
  </div>;
};

function App() {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <BrowserRouter>
        <Switch>
          <Route exact path="/" component={Home} />
          <Route path="/auth" component={LandingPage} />
        </Switch>
      </BrowserRouter>
    </div>
  ); 
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Registration.js

import React, { Component } from 'react';
import { Switch, Route, Link, Redirect } from 'react-router-dom';
const LoginRegister = (props) => {
  return (
    <div>
      Login or register
    <Link to="/login">Login</Link>
      <br />
      <Link to="/signup" >Signup</Link>
    </div>
  );
}

const Login = (props) =>{
  console.log("login ", props);
  return (
    <div>
      Login Component
    <Link to="/auth/signup" >Signup</Link>
    </div>
  );
} 

const Signup = () => (
  <div>
  Signup component
    <Link to="/auth/login" >Login</Link>
  </div>
);

class LandingPage extends Component {

  render() {
    console.log('Landing page',this.props);
    const loginPath = this.props.match.path +'/login';
    const signupPath = this.props.match.path + '/signup';
    console.log(loginPath);
    return (
      <div className="container" >
      Landing page
        <Switch>
          <Route path={loginPath} component={Login} />
          <Route path={signupPath} component={Signup} />
          <Route path="/" exact component={LoginRegister} />
        </Switch>
      </div>
    );
  }

}

export default LandingPage;

答案 2 :(得分:-1)

尝试以下

RegistrationRoutes.js

import Introduction from '../containers/Registration/Introduction';
import Login from '../containers/Login';

const Routes = (
  <React.Fragment>
    <Route path="/login" component={Login} key="login" />,
    <Route path="/registration/introduction" component={Introduction} key="registration-intro" />
  </React.Fragment>
);
export default Routes;