使用connected-react-router使router与store同步
connected-react-router是一个绑定react-router到redux的组件,来实现双向绑定router的数据到redux store中,这么做的好处就是让应用更Redux化,可以在action中实现对路由的操作。
这个组件的关键就在于使用了react-router中的一个关键组件,也就是ReactTraining/history,这个组件看了下文档,作者是这么解释的
The history library is a lightweight layer over browsers' built-in History and Location APIs. The goal is not to provide a full implementation of these APIs, but rather to make it easy for users to opt-in to different methods of navigation.
按照我的理解应该是对浏览器原本的history对象做了一定的增强,同时应该对ReactNative等环境做了一定的polyfill。
使用connected-react-router这个库的关键点就在于创建合适的history对象
我当前connected-react-router的版本为v6,需要react router大于v4,并且react-redux大于v6,react大于v16.4.0

先看index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux'
import configureStore, { history } from './configureStore'
import { ConnectedRouter } from 'connected-react-router'
import routes from './routes' const store = configureStore() ReactDOM.render(
<Provider store={store}>// Provider使用context将store传给子组件
<ConnectedRouter history={history}>//ConnectedRouter传递history对象作为props
{ routes }
</ConnectedRouter>
</Provider>
, document.getElementById('root'));
configureStore.js提供history与store
使用createBrowserHistory()创建history。
const history = createBrowserHistory()
使用redux-devtools-extension
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
reducers/index.js
import { combineReducers } from 'redux'
import { connectRouter } from 'connected-react-router'
import counterReducer from './counter'
const rootReducer = (history) => combineReducers({
count: counterReducer,
router: connectRouter(history)
})
export default rootReducer
combineReducers方法,用于 Reducer 的拆分。你只要定义各个子 Reducer 函数,然后用这个方法,将它们合成一个大的 Reducer
reducers/counter.js
const counterReducer = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
export default counterReducer
routes/index.js
components/Counter.js
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { increment, decrement } from '../actions/counter' const Counter = (props) => (
<div>
Counter: {props.count}
<button onClick={props.increment}>+</button>
<button onClick={props.decrement}>-</button>
</div>
) Counter.propTypes = {
count: PropTypes.number,
increment: PropTypes.func.isRequired,
decrement: PropTypes.func.isRequired,
} const mapStateToProps = state => ({
count: state.count,
}) const mapDispatchToProps = dispatch => ({
increment: () => dispatch(increment()),
decrement: () => dispatch(decrement()),
}) export default connect(mapStateToProps, mapDispatchToProps)(Counter)
actions/counter.js
export const increment = () => ({
type: 'INCREMENT',
})
export const decrement = () => ({
type: 'DECREMENT',
})
使用connected-react-router使router与store同步的更多相关文章
- [React] 10 - Tutorial: router
Ref: REACT JS TUTORIAL #6 - React Router & Intro to Single Page Apps with React JS Ref: REACT JS ...
- vue & vue router & dynamic router
vue & vue router & dynamic router https://router.vuejs.org/guide/essentials/dynamic-matching ...
- router.go,router.push,router.replace的区别
除了使用 <router-link> 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现.当你点击 <router-link> 时,这个 ...
- [Angular2 Router] Programmatic Router Navigation via the Router API - Relative And Absolute Router Navigation
In this tutorial we are going to learn how to navigate programmatically (or imperatively) by using t ...
- vue router.push(),router.replace(),router.go()和router.replace后需要返回两次的问题
转载:https://www.cnblogs.com/lwwen/p/7245083.html https://blog.csdn.net/qq_15385627/article/details/83 ...
- vue router.push(),router.replace(),router.go()
1.router.push(location)=====window.history.pushState 想要导航到不同的 URL,则使用 router.push 方法.这个方法会向 history ...
- [Angular2 Router] Index router
Index router as default router. import {RouterModule} from "@angular/router"; import {NotF ...
- $router和router区别
this.$router.push({path:'/'})//这个是js编程式的一种动态跳转路由方式,是全局的路由对象. 而写在router声明文件中的router是自己定义实例化的一个对象.可以使用 ...
- React中setState 什么时候是同步的,什么时候是异步的?
class Example extends React.Component { constructor() { super(); this.state = { val: 0 }; } componen ...
随机推荐
- R 《回归分析与线性统计模型》page93.6
rm(list = ls()) #数据处理 library(openxlsx) library(car) library(lmtest) data = read.xlsx("xiti4.xl ...
- 004.Oracle数据库 , 查询多字段连接合并
/*Oracle数据库查询日期在两者之间*/ SELECT PKID , OCCUR_DATE, PKID || TO_CHAR( OCCUR_DATE, ' yyyy/mm/dd hh24:mi:s ...
- 7.11 如何应用Varnish
动态数据缓存 Step 1 修改devault.vcl文件 # This ) # man page for details on VCL syntax and semantics. # # Defau ...
- nginx配置多个项目
使用Nginx要在同一个域名下配置多个项目有两种方式: nginx按不同的目录分发给不同的项目 启用二级域名,不同的项目分配不同的二级域名 1.nginx按不同的目录分发给不同的项目: server ...
- UVA - 10305 Ordering Tasks(拓扑排序)
题意:给定优先关系进行拓扑排序. 分析:将入度为0的点加入优先队列,并将与之相连的点入度减1,若又有度数为0的点,继续加入优先队列,依次类推. #pragma comment(linker, &quo ...
- 微信小程序实现左滑删除效果(原生/uni-app)
实现效果 列表中侧滑删除 删除不同时存在 scrollview上下滑动与侧滑删除不影响 uni-app实现 html部分 <template> <scroll-view :scrol ...
- Fedora Workstation 31众多功能得到改进
导读 周一,Red Hat的桌面高级经理Christian F.K. Schaller分享了一篇博客文章,概述了Fedora Workstation 31的各种改进和特性.这些包括Wayland的改进 ...
- 二十三、JavaScript之html事件
一.代码如下 二.效果如下 三.点击之后 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" ...
- 八十五、SAP中的ALV创建之四,去掉主键背景色
一.我们执行程序之后,发现表格中所有的内容都是有底色的 二.这个是因为我们在代码中规定了主键 三.我们去掉处理第一个之外的主键 四.运行程序,发现还是所有字段都有背景色 五.这是因为在每次ls_fie ...
- 无法启动APK安装也,报异常FileUriExposedException
无法打开APK安装页,报异常FileUriExposedException, https://juejin.im/entry/58e4643db123db15eb79a902