[Redux] Navigating with React Router <Link>
We will learn how to change the address bar using a component from React Router.
In Root.js:
We need to add a param to change the Route:
import React from 'react';
import {Provider} from 'react-redux';
import {Router, Route, browserHistory } from 'react-router';
import App from './App'; const Root = ({ store }) => (
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/(:filter)" component={App}/>
</Router>
</Provider>
) export default Root;
(:filter) means: it might not exisits.
In Foot.js, displaying the filter link like this;
const Footer = () => (
<p>
Show:
{" "}
<FilterLink filter="SHOW_ALL">
All
</FilterLink>
{", "}
<FilterLink filter="SHOW_ACTIVE">
Active
</FilterLink>
{", "}
<FilterLink filter="SHOW_COMPLETED">
Completed
</FilterLink>
</p>
);
We want to change it little bit, so it would be more url friendly:
<p>
Show:
{" "}
<FilterLink filter="all">
All
</FilterLink>
{", "}
<FilterLink filter="active">
Active
</FilterLink>
{", "}
<FilterLink filter="completed">
Completed
</FilterLink>
</p>
In the FilterLink.js:
We rewrite the code by using <Link> from 'react-router':
import React from 'react';
import { Link } from 'react-router'; const FilterLink = ( {filter, children} ) => (
<Link
to={filter === 'all' ? '' : filter}
activeStyle={{
textDecoration: 'none',
color: black
}}
>
{children}
</Link>
); export default FilterLink;
If the filter is 'all', then just use default url.
If the link is actived, then use the activeStyle.
And we can delete the action creator for setVisibilityFilter in actions.js:
//delete
export const setVisibilityFilter = (filter) => ({
type: 'SET_VISIBILITY_FILTER',
filter,
});
Aslo delete the Link.js, it is not needed anymore, we use the Link component from react-router.
[Redux] Navigating with React Router <Link>的更多相关文章
- [Redux] Filtering Redux State with React Router Params
We will learn how adding React Router shifts the balance of responsibilities, and how the components ...
- [Redux] Adding React Router to the Project
We will learn how to add React Router to a Redux project and make it render our root component. Inst ...
- [React] React Router: Router, Route, and Link
In this lesson we'll take our first look at the most common components available to us in react-rout ...
- 最新的chart 聊天功能( webpack2 + react + router + redux + scss + nodejs + express + mysql + es6/7)
请表明转载链接: 我是一个喜欢捣腾的人,没事总喜欢学点新东西,可能现在用不到,但是不保证下一刻用不到. 我一直从事的是依赖angular.js 的web开发,但是我怎么能一直用它呢?看看最近火的一塌糊 ...
- [React Router v4] Style a Link that is Active with NavLink
We often need to be able to apply style to navigation links based on the current route. In React Rou ...
- [React Router v4] Use the React Router v4 Link Component for Navigation Between Routes
If you’ve created several Routes within your application, you will also want to be able to navigate ...
- 关于react router 4 的小实践
详细代码栗子:https://github.com/wayaha/react-dom-CY clone然后 npm install npm start 分割线 1.这个项目使用create-react ...
- React Router 4.x 开发,这些雷区我们都帮你踩过了
前言 在前端框架层出不穷的今天,React 以其虚拟 DOM .组件化开发思想等特性迅速占据了主流位置,成为前端开发工程师热衷的 Javascript 库.作为 React 体系中的重要组成部分:Re ...
- React Router API文档
React Router API文档 一.<BrowserRouter> 使用HTML5历史记录API(pushState,replaceState和popstate事件)的<Rou ...
随机推荐
- nginx服务器,php-fpm重启
1.重启nginx服务器:首先whereis nginx找到你的nginx命令执行文件所在目录,直接/usr/local/nginx/sbin/nginx -s reload 这个路径可能每个人不一样 ...
- Window 下安装Redis
下载地址:https://github.com/dmajkic/redis/downloads. 下载到的Redis支持32bit和64bit.根据自己实际情况选择,将64bit的内容cp到自定义盘符 ...
- InstallShield 创建自己的Dialog
1.在"User Interface"-"Dialogs"下,在All Dialogs右击"New Dialogs-"创建自己的Dialog ...
- Makefile的简单例子
1.生成test可执行文件,源文件有prog.c prog.h cord.h test:prog.o code.o gcc -o test prog.o code.o prog.o:prog.c pr ...
- Sublime text 3 快键方式汇总 及 主题应用
Sublime Text 3 快捷键汇总 Sublime Text 3是款非常实用代码编辑神器,但是想要用任何一款软件,掌握一些快捷键还是很有必要的. 选择类 Ctrl+D 选中光标所占的文本,继续操 ...
- QueryRunner的使用
在相继学习了JDBC和数据库操作之后,我们明显感到编写JDBC代码并非一件轻松的事儿.为了帮助我们更高效的学习工作,从JDBC的繁重代码中解脱出来,老佟给我们详尽介绍了一个简化JDBC操作的组件——D ...
- Jsp中获得集合List或Set的长度
首先要引入<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> ...
- Hibernate Is Not Mapped(实体名 is not mapped [from book where id='0'])
org.springframework.orm.hibernate3.HibernateQueryException: USERINFO is not mapped.看到.hbm.xml文件中的< ...
- div 背景色设置_DIV背景颜色设置
DIV 背景色设置篇-div背景颜色设置篇 一.div标签内直接设置背景颜色 - TOP <div style="background:#000; color:#FFF&quo ...
- Error: opening registry key 'Software\JavaSoft\Java Runtime Environment' Error: could not find java.dll
java -jar yxCollector-1.1.0.jarError: opening registry key 'Software\JavaSoft\Java Runtime Environme ...