Decoupling route declarations

The difficulty with routing happens when your application has dozens of routes declared within a single module, because it's more difficult to mentally map routes to features.

To help with this, each top-level feature of the application can define its own routes. This way, it's clear which routes belong to which feature. So, let's start with the App component:

import React, { Fragment } from 'react';
import {
BrowserRouter as Router,
Route,
Redirect
} from 'react-router-dom';

// Import the routes from our features.
import One from './one';
import Two from './two';

// The feature routes are rendered as children of
// the main router.
export default () => (
<Router>
<Fragment>
<Route exact path="/" render={() => <Redirect to="one" />} />
<One />
<Two />
</Fragment>
</Router>
);

In this example, the application has two features: one and two. These are imported as components and rendered inside <Router>. You have to include the <Fragment> element because <Router> doesn't like having multiple children. By using a fragment, you're passing one child without having to use an unnecessary DOM element. The first child in this router is actually a redirect. This means that when the app first loads the URL /, the <Redirect> component will send the user to /one. The render property is an alternative to the component property when you need to call a function to render content. You're using it here because you need to pass the property to <Redirect>.

This module will only get as big as the number of application features, instead of the number of routes, which could be substantially larger. Let's take a look at one of the feature routes:

import React, { Fragment } from 'react';
import { Route, Redirect } from 'react-router';

// The pages that make up feature "one".
import First from './First';
import Second from './Second';

// The routes of our feature. The "<Redirect>"
// handles "/one" requests by redirecting to "/one/1".
export default () => (
<Fragment>
<Route
exact
path="/one"
render={() => <Redirect to="/one/1" />}
/>
<Route exact path="/one/1" component={First} />
<Route exact path="/one/2" component={Second} />
</Fragment>
);

This module, one/index.js, exports a component that renders a fragment with three routes:

  • When the path /one is matched, redirect to /one/1
  • When the path /one/1 is matched, render the First component
  • When the path /one/2 is matched, render the Second component

This follows the same pattern as the App component for the path /. Often, your application doesn't actually have content to render at the root of a feature or at the root of the application itself. This pattern allows you to send the user to the appropriate route and the appropriate content. Here's what you'll see when you first load the app:

The second feature follows the exact same pattern as the first. Here's what the component first looks like:

import React from 'react';

export default () => (
<p>Feature 1, page 1</p>
);

Each feature in this example uses the same minimal rendered content. These components are ultimately what the user needs to see when they navigate to a given route. By organizing routes this way, you've made your features self-contained with regard to routing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.153.38