React路由的使用
使用React路由之前,我们需要先安装 react-router-dom
这个包。比如:
yarn add react-router-dom
代码举例: (1)index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- 容器,通过 React 渲染得到的 虚拟DOM,会呈现到这个位置 -->
<div id="app"></div>
</body>
</html>
(2)main.js:
// JS打包入口文件
// 1. 导入包
import React from "react";
import ReactDOM from "react-dom";
import App from "./App.jsx";
// 使用 render 函数渲染 虚拟DOM
ReactDOM.render(<App />, document.getElementById("app"));
(3)app.jsx:
import React from "react";
// 如果要使用 路由模块,第一步,运行 yarn add react-router-dom
// 第二步,导入 路由模块
// HashRouter 表示一个路由的跟容器,将来,所有的路由相关的东西,都要包裹在 HashRouter 里面,而且,一个网站中,只需要使用一次 HashRouter 就好了;
// Route 表示一个路由规则, 在 Route 上,有两个比较重要的属性, path component
// Link 表示一个路由的链接 ,就好比 vue 中的 <router-link to=""></router-link>
import { HashRouter, Route, Link } from "react-router-dom";
import Home from "./components/Home.jsx";
import Movie from "./components/Movie.jsx";
import About from "./components/About.jsx";
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
// 当 使用 HashRouter 把 App 根组件的元素包裹起来之后,网站就已经启用路由了
// 在一个 HashRouter 中,只能有唯一的一个根元素
// 在一个网站中,只需要使用 唯一的一次 <HashRouter></HashRouter> 即可
return (
<HashRouter>
<div>
<h1>这是网站的APP根组件</h1>
<hr />
<Link to="/home">首页</Link>
<Link to="/movie">电影</Link>
<Link to="/about">关于</Link>
<hr />
{/* Route 创建的标签,就是路由规则,其中 path 表示要匹配的路由,component 表示要展示的组件 */}
{/* 在 vue 中有个 router-view 的路由标签,专门用来放置,匹配到的路由组件的,但是,在 react-router 中,并没有类似于这样的标签,而是 ,直接把 Route 标签,当作的 坑(占位符) */}
{/* Route 具有两种身份:1. 它是一个路由匹配规则; 2. 它是 一个占位符,表示将来匹配到的组件都放到这个位置 */}
<Route path="/home" component={Home} />
<hr />
<Route path="/movie" component={Movie} />
<hr />
<Route path="/about" component={About} />
</div>
</HashRouter>
);
}
}
(4)ReactDemo/src/components/Home.jsx
import React from "react";
export default class Home extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return <div>Home组件</div>;
}
}
(5)ReactDemo/src/components/Movie.jsx
import React from "react";
export default class Movie extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return <div>Movie组件</div>;
}
}
(6)ReactDemo/src/components/About.jsx
import React from "react";
export default class About extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return <div>About组件</div>;
}
}
运行结果: 20190214_1000.png
匹配路由参数
模糊匹配与精准匹配
我们在上面的代码中,进一步修改。假设 Movie 这个组件修改成这种路由匹配方式:
<Link to="/movie/top250">电影</Link>
<Route path="/movie" component={Movie} />
上面这种匹配方式,也是可以成功匹配到的。这是为啥呢?
这是因为:默认情况下,路由中的匹配规则,是模糊匹配的。如果 路由可以部分匹配成功,就会展示这个路由对应的组件。
如果想让路由规则,进行精确匹配,可以为Route添加 exact
属性。比如下面这种写法,因为是开启了精准匹配,所以是匹配不到的:(无法匹配)
<Link to="/movie/top250/20">电影</Link>
<Route path="/movie/" component={Movie} exact/>
另外,如果要匹配参数,可以在匹配规则中,使用 :
修饰符,表示这个位置匹配到的是参数。举例如下:(匹配正常)
<Link to="/movie/top250/20">电影</Link>
<Route path="/movie/:type/:id" component={Movie} exact/>
获取路由参数
继续修改上面的代码。如果我想在 Movie 组件中显示路由中的参数,怎么做呢?
我们可以通过 props.match.params
获取路由中的参数。举例做法如下:
app.jsx中的匹配规则如下:
<Link to="/movie/top100/5">电影</Link>
<Route path="/movie/:type/:id" component={Movie} exact/>
Moivie 组件的写法如下:
import React from "react";
export default class Movie extends React.Component {
constructor(props) {
super(props);
this.state = {
routeParams: props.match.params // 把路由中的参数保存到 state 中
};
}
render() {
console.log(this);
// 如果想要从路由规则中,提取匹配到的参数,进行使用,可以使用 this.props.match.params.*** 来访问
return (
<div>
{/* Movie --- {this.props.match.params.type} --- {this.props.match.params.id} */}
Movie --- {this.state.routeParams.type} --- {this.state.routeParams.id}
</div>
);
}
}
Beginner’s Guide to React Router
React is a JavaScript library for building user interfaces. We can also extend it to build multi-page applications with the help of React Router. This is a third-party library that enables routing in our React apps. In this tutorial, we are going to cover everything you need to know to get started with React Router.
Setting up the project
To be able to follow along, you will need to create a new React app by running the following command in your terminal:
npx create-react-app react-router-guide
Then, add these lines of code to the App.js
file:
import React from "react";
import "./index.css"
export default function App() {
return (
<main>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
</main>
);
}
// Home Page
const Home = () => (
<Fragment>
<h1>Home</h1>
<FakeText />
</Fragment>
);
// About Page
const About = () => (
<Fragment>
<h1>About</h1>
<FakeText />
</Fragment>
);
// Contact Page
const Contact = () => (
<Fragment>
<h1>Contact</h1>
<FakeText />
</Fragment>
);
const FakeText = () => (
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
)
Then, if you’re ready to go, let’s start by answering an important question: what is routing?
What is routing?
Routing is the capacity to show different pages to the user. That means the user can move between different parts of an application by entering a URL or clicking on an element. As you may already know, by default, React comes without routing. And to enable it in our project, we need to add a library named react-router. To install it, you will have to run the following command in your terminal:
yarn add react-router-dom
Or
npm install react-router-dom
Now, we’ve successfully installed our router, let’s start using it in the next section.
Setting up the router
To enable routing in our React app, we first need to import BrowserRouter
from react-router-dom
.
In the App.js
file, enter the following:
import React, { Fragment } from "react";
import "./index.css"
import { BrowserRouter as Router } from "react-router-dom";
export default function App() {
return (
<Router>
<main>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
</main>
</Router>
);
}
This should hold everything in our app where routing is needed. That means, if we need routing in our entire app, we must wrap our higher component with BrowserRouter
.
By the way, you don’t have to rename BrowserRouter as Router
as I do here, I just want to keep things readable.
A router alone doesn’t do much. So let’s add a route in the next section.
Rendering routes
To render routes, we have to import the Route
component from the router package.
In your App.js
file, add the following code:
import React, { Fragment } from "react";
import "./index.css"
import { BrowserRouter as Router, Route } from "react-router-dom";
export default function App() {
return (
<Router>
<main>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
<Route path="/" render={() => <h1>Welcome!</h1>} />
</main>
</Router>
);
}
Then, add it where we want to render the content. The Route
component has several properties. But here, we just need path
and render
.
path
: the path of the route. Here, we use /
to define the path of the home page.
render
: will display the content whenever the route is reached. Here, we’ll render a welcome message to the user.
In some cases serving routes like that is perfectly fine. But imagine a case when we have to deal with a real component – using render
may not be the right solution.
So, how can we display a real component? Well, the Route
component has another property named component
.
Let’s update our example a bit to see it in action.
In your App.js
file, add the following code:
import React, { Fragment } from "react";
import "./index.css"
import { BrowserRouter as Router, Route } from "react-router-dom";
export default function App() {
return (
<Router>
<main>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
<Route path="/" component={Home} />
</main>
</Router>
);
}
const Home = () => (
<Fragment>
<h1>Home</h1>
<FakeText />
</Fragment>
);
Now, instead of rendering a message, our route will load the Home
component.
To get the full power of React Router, we need to have multiple pages and links to play with. We already have pages (components if you want, too), so now let’s add some links so we can switch between pages.
Using links to switch pages
To add links to our project, we will use the React Router again.
In your App.js
file, add the following code:
import React, { Fragment } from "react";
import "./index.css"
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
export default function App() {
return (
<Router>
<main>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/contact">Contact</Link></li>
</ul>
</nav>
<Route path="/" exact component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</main>
</Router>
);
}
const Home = () => (
<Fragment>
<h1>Home</h1>
<FakeText />
</Fragment>
);
const About = () => (
<Fragment>
<h1>About</h1>
<FakeText />
</Fragment>
);
const Contact = () => (
<Fragment>
<h1>Contact</h1>
<FakeText />
</Fragment>
);
After importing Link
, we have to update our navigation bar a bit. Now, instead of using a
tag and href
, React Router uses Link
and to
to, well, be able to switch between pages without reloading it.
Then, we need to add two new routes, About
and Contact
, to be able to switch between pages or components.
Now, we can go to different parts of our app through links. But there is an issue with our router: the Home
component is always displayed even if we switch to other pages.
This is because React Router will check if the path
defined starts with /
. If that’s the case, it will render the component. And here, our first route starts with /
, so the Home
component will be rendered each time.
However, we can still change the default behavior by adding the exact
property to Route
.
In App.js
, add:
<Route path="/" exact component={Home} />
By updating the Home
route with exact
, now it will be rendered only if it matches the full path.
We can still enhance it by wrapping our routes with Switch
to tell to React Router to load only one route at a time.
In App.js
, add:
import { BrowserRouter as Router, Route, Link, Switch } from "react-router-dom";
<Switch>
<Route path="/" exact component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</Switch>
Now that we have new links, let’s use them to pass parameters.
Passing route parameters
To pass data between pages, we have to update our example.
In your App.js
file, add the following code:
import React, { Fragment } from "react";
import "./index.css"
import { BrowserRouter as Router, Route, Link, Switch } from "react-router-dom";
export default function App() {
const name = 'John Doe'
return (
<Router>
<main>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to={`/about/${name}`}>About</Link></li>
<li><Link to="/contact">Contact</Link></li>
</ul>
</nav>
<Switch>
<Route path="/" exact component={Home} />
<Route path="/about/:name" component={About} />
<Route path="/contact" component={Contact} />
</Switch>
</main>
</Router>
);
}
const Home = () => (
<Fragment>
<h1>Home</h1>
<FakeText />
</Fragment>
);
const About = ({match:{params:{name}}}) => (
// props.match.params.name
<Fragment>
<h1>About {name}</h1>
<FakeText />
</Fragment>
);
const Contact = () => (
<Fragment>
<h1>Contact</h1>
<FakeText />
</Fragment>
);
As you can see here, we start by declaring a new constant name
which will be passed as a parameter to the About
page. And we append name
to the corresponding link.
With that, we now have to update the About
route by adjusting its path to receive name
as a parameter path="/about/:name"
.
Now, the parameter will be received as props from the About
component. The only thing we have to do now is destructure the props and get back the name
property. By the way, {match:{params:{name}}}
is the same as props.match.params.name
.
We’ve done a lot up to this point. But in some cases we don’t want to use links to navigate between pages.
Sometimes, we have to wait for an operation to finish before navigating to the next page.
So, let’s handle that case in the next section.
Navigating programmatically
The props we receive have some convenient methods we can use to navigate between pages.
In App.js
, add:
const Contact = ({history}) => (
<Fragment>
<h1>Contact</h1>
<button onClick={() => history.push('/') } >Go to home</button>
<FakeText />
</Fragment>
);
Here, we pull the history
object from the props we receive. It has some handy methods like goBack
, goForward
, and so on. But here, we will use the push
method to be able to go to the Home page.
Now, let’s handle the case when we want to redirect our user after an action.
Redirecting to another page
The React Router has another component named Redirect
. As you guessed, it helps us redirect the user to another page
In App.js
, add:
import { BrowserRouter as Router, Route, Link, Switch, Redirect } from "react-router-dom";
const About = ({match:{params:{name}}}) => (
// props.match.params.name
<Fragment>
{ name !== 'John Doe' ? <Redirect to="/" /> : null }
<h1>About {name}</h1>
<FakeText />
</Fragment>
);
Now, if the name
passed as a parameter is not equal to John Doe
, the user will be redirected to the home page.
You could argue that you should redirect the user with props.history.push('/)
. Well, the Redirect
component replaces the page and therefore the user can’t go back to the previous page. But, with the push method they can. However, you can use props.history.replace('/)
to mimic the Redirect
behavior.
Now let’s move on and handle the case when the user hits a route that doesn’t exist.
Redirecting to a 404 page
To redirect the user to a 404 page, you can create a component to show it. But here, to keep things simple, I will just display a message with render
.
import React, { Fragment } from "react";
import "./index.css"
import { BrowserRouter as Router, Route, Link, Switch } from "react-router-dom";
export default function App() {
const name = 'John Doe'
return (
<Router>
<main>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to={`/about/${name}`}>About</Link></li>
<li><Link to="/contact">Contact</Link></li>
</ul>
</nav>
<Switch>
<Route path="/" exact component={Home} />
<Route path="/about/:name" component={About} />
<Route path="/contact" component={Contact} />
<Route render={() => <h1>404: page not found</h1>} />
</Switch>
</main>
</Router>
);
}
The new route we’ve added will catch every path that doesn’t exist and redirect the user to the 404 page. Now, let’s move on and learn how to protect our routes in the next section.
Guarding routes
There are many ways to protect routes to React. But here I will just check if the user is authenticated and redirect them to the appropriate page.
import React, { Fragment } from "react";
import "./index.css"
import { BrowserRouter as Router, Route, Link, Switch } from "react-router-dom";
export default function App() {
const name = 'John Doe'
const isAuthenticated = false
return (
<Router>
<main>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to={`/about/${name}`}>About</Link></li>
<li><Link to="/contact">Contact</Link></li>
</ul>
</nav>
<Switch>
<Route path="/" exact component={Home} />
{
isAuthenticated ?
<>
<Route path="/about/:name" component={About} />
<Route path="/contact" component={Contact} />
</> : <Redirect to="/" />
}
</Switch>
</main>
</Router>
);
}
As you can see here, I declared a variable to mimic authentication. Then, check if the user is authenticated or not. If they are, render protected pages. Otherwise redirect them to the home page. We’ve covered a lot up to this point, but an interesting part remains: router hooks. Let’s move to the final section and introduce Hooks.
Router Hooks
Router hooks make things much easier. Now you can access the history, location, or parameters in an easy and elegant way.
useHistory
The useHistory
hook gives us access to the history instance without pulling it from props.
import { useHistory } from "react-router-dom";
const Contact = () => {
const history = useHistory();
return (
<Fragment>
<h1>Contact</h1>
<button onClick={() => history.push('/') } >Go to home</button>
</Fragment>
)
};
useParams
This hook helps us get the parameter passed on the URL without using the props object.
import { BrowserRouter as Router, Route, Link, Switch, useParams } from "react-router-dom";
export default function App() {
const name = 'John Doe'
return (
<Router>
<main>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to={`/about/${name}`}>About</Link></li>
</ul>
</nav>
<Switch>
<Route path="/" exact component={Home} />
<Route path="/about/:name" component={About} />
</Switch>
</main>
</Router>
);
}
const About = () => {
const { name } = useParams()
return (
// props.match.params.name
<Fragment>
{ name !== 'John Doe' ? <Redirect to="/" /> : null }
<h1>About {name}</h1>
<Route component={Contact} />
</Fragment>
)
};
useLocation
This hook returns the location object that represents the current URL.
import { useLocation } from "react-router-dom";
const Contact = () => {
const { pathname } = useLocation();
return (
<Fragment>
<h1>Contact</h1>
<p>Current URL: {pathname}</p>
</Fragment>
)
};
Final Thoughts
React Router is an amazing library that helps us go from a single page to a multi-page application feeling with great usability. (Just keep in mind – at the end of the day, it’s still a single page app). And now with router hooks, you can see how easy and elegant they are. They’re definitely something to consider in your next project.