- React Router Quick Start Guide
- Sagar Ganatra
- 617字
- 2021-07-23 16:41:32
Defining application routes
The react-router-dom package includes a <BrowserRouter> component, which is used as a wrapper before adding routes in the application. To use React-Router in the React Native application, the react-router-native package is used. This will be discussed in detail in later chapters. The <BrowserRouter> component is an implementation of the router interface that makes use of HTML5's history API to keep the UI in sync with the URL path.
The first step is to wrap the application's root component with <BrowserRouter>, as shown here:
import { BrowserRouter } from 'react-router-dom';
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById('root')
);
Wrapping your application inside <BrowserRouter> will create an instance of history for our <App> component, giving all of its child components access to props from the native browser history API. This allows components to match against URL paths and render the appropriate page component.
Routing in React-Router isn't actually routing—it's conditional rendering of components based on the pattern that matches with the current URL path. To define a route, we need two pieces of information: the URL path to match with and the component to render. Let's create two components, HomeComponent and DashboardComponent, that render at /home and /dashboard respectively.
In src/components/home/home.component.js:
import React from 'react';
export const HomeComponent = () => (
<div>
Inside Home route
</div>
);
And in src/components/dashboard/dashboard.component.js:
import React from 'react';
export const DashboardComponent = () => (
<div className="dashboard">
Inside Dashboard route
</div>
);
The import statement is required since we are returning JSX from the preceding components.
The next step is to define a route using the Route component (from 'react-router-dom'). The Route component accepts several props, but for the purpose of this example, we will use path and component.
In App.js:
class App extends Component {
render() {
return (
<div className="container">
<Route
path="/home"
component={HomeComponent}
/>
<Route
path="/dashboard"
component={DashboardComponent}
/>
</div>
);
}
}
export default App;
Here, we're defining routes within the 'render' method of the <App> component. Each <Route> component has a path prop, which mentions the URL path to match, and a component prop, mentioning the component to render once the path matches the URL.
When you run the app (npm start) and visit localhost:3000/home, HomeComponent is rendered and the message Inside Home Component is displayed. Similarly, DashboardComponent is rendered when you visit localhost:3000/dashboard.
<BrowserRouter> creates a History object, which it uses to keep track of the current location and re-render the site whenever it changes. <BrowserRouter> makes the History object available to its descendent child components through React's context. A Route component that does not have <BrowserRouter> as its parent will fail to work.
Also, it's a requirement that <BrowserRouter> has only one child element. In the following snippet, <BrowserRouter> is given two child elements:
<BrowserRouter>
<Route
path="/home"
component={HomeComponent} />
<Route
path="/dashboard"
component={DashboardComponent} />
</BrowserRouter>
The preceding code will result in an error, such as A <Router> may have only one child element. To resolve this, you could either move these routes into a component and provide the component reference, or wrap the <Route> components in the preceding snippet inside another element, such as div or React Fragment.
Apart from BrowserRouter, there are other types of routers in the React-Router library: HashRouter, MemoryRouter, and StaticRouter. These are discussed in later chapters.
- Learn ECMAScript(Second Edition)
- 劍指Offer(專項突破版):數據結構與算法名企面試題精講
- Reactive Programming with Swift
- C語言從入門到精通(第4版)
- INSTANT Mercurial SCM Essentials How-to
- Asynchronous Android Programming(Second Edition)
- Visualforce Developer’s guide
- Cocos2d-x Game Development Blueprints
- 軟件工程基礎與實訓教程
- Learning Splunk Web Framework
- 算法圖解
- Python 快速入門(第3版)
- Moodle 3.x Developer's Guide
- C++從零開始學(視頻教學版)(第2版)
- 編程改變生活:用PySide6/PyQt6創建GUI程序(進階篇·微課視頻版)