8000 react-router学习 · Issue #19 · Vxee/articles · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

react-router学习 #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

< A240 div class="d-flex flex-items-center"> Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Vxee opened this issue Nov 17, 2018 · 0 comments
Open

react-router学习 #19

Vxee opened this issue Nov 17, 2018 · 0 comments

Comments

@Vxee
Copy link
Owner
Vxee commented Nov 17, 2018

由于我看的是4.X版本,有些用法和2、3版本有点区别。

Link 需要从react-router-dom中去引用。

Route不允许这样嵌套。

<Route path="/" component={Home} >
	<Route path="about" component={About} />
	<Route path="inbox" component={Inbox} />
	<Route render={() => <h6>404</h6>} />
</Route>

应该是平级的书写方式

<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/inbox" component={Inbox} />
<Route render={() => <h6>404</h6>} />

可以嵌套一个Switch来只匹配一个组件渲染。

import { Route } from 'react-router';
import { Switch } from 'react-router-dom';

<Switch>
	<Route exact path="/" component={Home} />
    <Route path="/about" component={About} />
    <Route path="/inbox" component={Inbox} />
    // 当没有任何匹配时,会渲染一个404
    <Route render={() => <h6>404</h6>} />
</Switch>

路径匹配语法

<Route path="/hello/:name">         // 匹配 /hello/michael 和 /hello/ryan
<Route path="/hello(/:name)">       // 匹配 /hello, /hello/michael 和 /hello/ryan
<Route path="/files/*.*">           // 匹配 /files/hello.jpg 和 /files/path/to/hello.jpg

withRouter

import { withRouter } from 'react-router';

const ShowTheLocation = ({ match, location, history}) => {
    return (
    	<div>You are now at {location.pathname}</div>
    );
}
export default withRouter(ShowTheLocation);

withRouter是一个hoc,可以把路由的一些信息当作props传递到组件中,并返回一个新的组件。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0