React-router v4教程
在这个教程里,我们会从一个例子React应用开始学习react-router-dom。其中你会学习如何使用 也许学习react-router最好的办法就是用react-router-dom v4来写一个多页的react应用。这个react应用会包含登录、注册、首页、联系人等页面。但是,首先让我们来看一下react router v4的概念,以及它与v3有什么不同的地方。 React router v4 vs v3v4是react router的一次重写,所以和v3有很多不同的地方。主要有:
React-router vs react-router-dom vs react-router-native
安装如上所说,我们使用react开发web应用,所以只需要安装 npm install react-router-dom --save 理解和使用react-router
BrowserRouter vs HashRouter在react-router的各种router中, 理解和使用Route<Route>组件是react router v4里最有用的组件。背后的使用哲学也很简单,无论何时你需要在匹配某个路径的时候绘制一个组件,那么就可以使用
还有其他的一些属性,可以用来代替
多数的时候是用
如: <Route exact path="/" component={HomePage} /> 使用 <Route path="/" render={()=><div>HomePage</div>} /> 来看哥更复杂的: const FadingRoute = ({ component,...rest }) => ( <Route {...rest} render={(props) => ( <FadeIn> <componnet {...props} /> </FadeIn> )} /> ) <FadingRoute path="/cool" component={Something} /> 使用 <ul> <ListItemLink to="/somewhere" /> <LinkItemLink to="/somewhere-else" /> </ul> const ListItemLink = ({to,...rest}) => ( <Route path={to} children={({math}) => ( <li className={match ? 'active' : ''}> <Link to={to} {...rest} /> </li> )} /> ) 更多关于react-router v4如何匹配路径的内容,请移步这里。 URL / Path / Route的参数通常情况下,我们都会在路径里添加参数。这样方便在不同的组件之间传递一些必要的数据。那么我们如何才能获取到这些传递的参数,并传递给组件中呢?我们只需要在路径的最后加上 <Route path="/:param1" component={HomePage} /> const HomePage = ({match}) => ( <div> <h1> parameter => {match.params.param1} </div> ); 一旦有路径可以匹配成功,那么就会穿件一个拥有如下属性的对象,并传入绘制的组件里:
理解并使用Link
import { Link } from 'react-router-dom'; const Nav = () => ( <Link to '/'>Home</Link> ); 当被点击的时候,会跳转到路径:
<Link to{{ pathname: '/me',search: '?sort=asc',hash: '#hash',state: { fromHome: true } }} />
<Link>和<NavLink>
<NavLink to="/me" activeStyle={{SomeStyle}} activeClassName="selected"> My Profile </NavLink> 使用react router dom实现你的第一个demo现在我们用react router dom来实现第一个demo。 首先,引入必要的组件。比如: import { BrowserRouter,Route } from 'react-router-dom'; 接下来,我们创建一些组件和一些Html标签。同时我们用react router v4里的 const BaseLayout = () => ( <div className="base"> <header> <p>React Router v4 Browser Example</p> <nav> <ul> <li><Link ="/">Home</Link></li> <li><Link ="/about">About</Link></li> <li><Link ="/me">Profile</Link></li> <li><Link ="/login">Login</Link></li> <li><Link ="/register">Register</Link></li> <li><Link ="/contact">Contact</Link></li> </ul> </nav> </header> <div className="container"> <Route path="/" exact component={HomePage} /> <Route path="/about" component={AboutPage} /> <Route path="/contact" component={ContactPage} /> <Route path="/login" component={LoginPage} /> <Route path="/register" component={RegisterPage} /> <Route path="/me" component={ProfilePage} /> </div> <footer> React Router v4 Browser Example (c) 2017 </footer> </div> ); 然后我们来创建需要的组件: const HomePage = () => <div>This is a Home Page</div> const LoginPage = () => <div>This is a Login Page</div> const RegisterPage = () => <div>This is a Register Page</div> const ProfilePage = () => <div>This is a Profile Page</div> const AboutPage = () => <div>This is a About Page</div> const ContactPage = () => <div>This is a Contact Page</div> 最后,写 const App = () => ( <BrowserRouter> <BaseLayout /> </BrowserRouter> ) render(<App />,document.getElementById('root')); 如你所见,react router v4的组件还非常的易用的。 理解和使用非排他的路由在上例中,我们在 <Route path="/" exact component={HomePage} /> 这是因为v4中的路由默认都是非排他的,这一点和v3的实现思路截然不同。如果没有 如,当用户点了登录连接以后, 现在我们来看看非排他的路由有什么优点。假如我们有一个子菜单组件需要显示在profile页面出现的时候也出现。我们可以简单的修改 const BaseLayout = () => ( <div className="base"> <header> <p>React Router v4 Browser Example</p> <nav> <ul> <li><Link to="/">Home</Link></li> <li><Link to="/about">About</Link></li> <li> <Link to="/me">Profile</Link> <Route path="/me" component={ProfileMenu} /> </li> {/*略*/} </ul> </nav> </header> </div> ); 这样我们就会看到对应于 理解排他路由排他路由是react router v3的默认实现。只有第一个匹配的路由对应的组件会被绘制。这一点也可以用react router v4的 import { Switch,Route } from 'react-router'; <Switch> <Route exact path="/" component={HomePage} /> <Route path="/about" component={AboutPage} /> <Route path="me" component={ProfilePage} /> <Route component={NotFound} /> </Switch> 浏览器历史react router v4中,提供了一个 你也可以在React应用里使用 history.push("/my-path") history.replace("/my-path") 用另外的方法可以写成: <Link to="/my-path" /> <Redirect to="my-path" /> 使用<Redirect>组件实现重定向无论何时你要重定向到另外一个地址的时候,都可以使用 <Redirect to {{ pathname: '/register',search: '?utm=something',state: { referrer: someplage.com } }}> 或者,更为简单的: <Redirect to="register" /> 最后react router v4让开发react应用变得更加的简单。让react应用内的页面跳转更加简单。你只需要声明一个 原文地址:https://www.techiediaries.com... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |