reducer与按需加载组件的时候,一并加载对应的state,具体流程就不多说了,看代码!

reducer

import { combineReducers } from 'redux'
import { routerReducer } from 'react-router-redux' export const makeRootReducer = asyncReducers => {
return combineReducers({
routing: routerReducer,
...asyncReducers
})
} export const injectReducer = (store, {key, reducer}) => {
if(!store.asyncReducers[key]) {
store.asyncReducers[key] = reducer;
store.replaceReducer(makeRootReducer(store.asyncReducers));
}
}

store

import { applyMiddleware, compose, createStore, combineReducers } from 'redux'
import { routerMiddleware } from 'react-router-redux'
import thunkMiddleware from 'redux-thunk'
import reducer, { makeRootReducer } from './reducer' export default (initialState = {}, history) => {
const middleware = [thunkMiddleware, routerMiddleware(history)];
const enhancers = [];
const store = createStore(
makeRootReducer(),
initialState,
compose(
applyMiddleware(...middleware),
...enhancers
)
);
return store;
}

router

import React, { Component } from 'react'
import { Router, Route, Redirect } from 'react-router'
const moduleRoute = require.context('../view', true, /router$/) //获取view视图下,所有router文件
const router = store => {
return <Router>
<Route path="/">
{
moduleRoute.keys().map(key => {
return moduleRoute(key).default(store)
})
}
<Redirect from='*' to='/' />
</Route>
</Router>
} export default router

入口文件app.js

import ReactDOM from 'react-dom'
import { Router, hashHistory } from 'react-router'
import React from 'react'
import { Provider } from 'react-redux'
import { syncHistoryWithStore } from 'react-router-redux'
import createStore from '...上面的store'
import router from '...上面的router' const store = createStore({}, hashHistory);
store.asyncReducers = {};
const history = syncHistoryWithStore(hashHistory, store);
ReactDOM.render((
<Provider store={store}>
<Router history={history}>
{ router(store) }
</Router>
</Provider>
), document.getElementById("root"))

在view层级下创建一个test文件夹来编辑一下流程 

test/ index.jsx 中简单编辑下

import React, { Component } from 'react'
import { connect } from 'react-redux' class Test extends Component {
render() {
const { value } = this.props;
return <h1>{ value }</h1>
}
} const mapStateToProps = state => {
return { ...state.test }
} const mapDispathToProps = dispatch => {
return {
//
}
} export default connect(mapStateToProps, mapDispathToProps)(Test);

reducer

const initState = { value: 'value' }
export default (state = initState, action) => {
return state;
}

router

import { Route } from 'react-router'
import { injectReducer } from '...最上面定义的reducer'
export default store => {
return <Route
path='test'
getComponent={(nextState, cb) => {
import('../'/* webpackChunkName: 'Test' */)
.then(module => {
injectRoducer(store, {key: test, reducer: require('../redux/reducer')});
cb(null, module.default);
})
}
}/>
}

执行,在未加载该页面之前,store.state = { routing… }; 
进入test页面的时候, store.state = { routing…, test: { value: ‘value’ } }

react-router-redux的更多相关文章

  1. 最新的chart 聊天功能( webpack2 + react + router + redux + scss + nodejs + express + mysql + es6/7)

    请表明转载链接: 我是一个喜欢捣腾的人,没事总喜欢学点新东西,可能现在用不到,但是不保证下一刻用不到. 我一直从事的是依赖angular.js 的web开发,但是我怎么能一直用它呢?看看最近火的一塌糊 ...

  2. [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 ...

  3. [Redux] Filtering Redux State with React Router Params

    We will learn how adding React Router shifts the balance of responsibilities, and how the components ...

  4. [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 ...

  5. 实例讲解react+react-router+redux

    前言 总括: 本文采用react+redux+react-router+less+es6+webpack,以实现一个简易备忘录(todolist)为例尽可能全面的讲述使用react全家桶实现一个完整应 ...

  6. 基于 React.js + Redux + Bootstrap 的 Ruby China 示例 (转)

    一直学 REACT + METEOR 但路由部分有点问题,参考一下:基于 React.js + Redux + Bootstrap 的 Ruby China 示例 http://react-china ...

  7. 基于react+react-router+redux+socket.io+koa开发一个聊天室

    最近练手开发了一个项目,是一个聊天室应用.项目虽不大,但是使用到了react, react-router, redux, socket.io,后端开发使用了koa,算是一个比较综合性的案例,很多概念和 ...

  8. 关于react router 4 的小实践

    详细代码栗子:https://github.com/wayaha/react-dom-CY clone然后 npm install npm start 分割线 1.这个项目使用create-react ...

  9. React Router 4.x 开发,这些雷区我们都帮你踩过了

    前言 在前端框架层出不穷的今天,React 以其虚拟 DOM .组件化开发思想等特性迅速占据了主流位置,成为前端开发工程师热衷的 Javascript 库.作为 React 体系中的重要组成部分:Re ...

  10. React Router API文档

    React Router API文档 一.<BrowserRouter> 使用HTML5历史记录API(pushState,replaceState和popstate事件)的<Rou ...

随机推荐

  1. Spring框架针对dao层的jdbcTemplate操作crud之add添加数据库操作

    使用jdbcTemplate 原理是把加载驱动Class.forName("com.mysql.jdbc.Driver"); 和连接数据库Connection conn=Drive ...

  2. 最小生成树 Prim算法 Kruskal算法实现

    最小生成树定义 最小生成树是一副连通加权无向图中一棵权值最小的生成树. 在一给定的无向图 G = (V, E) 中,(u, v) 代表连接顶点 u 与顶点 v 的边(即,而 w(u, v) 代表此边的 ...

  3. C语言int *a 和int* a的写法

  4. 如何用纯 CSS 创作炫酷的同心矩形旋转动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/bMvbRp 可交互视频教 ...

  5. tornado框架基础04-模板基础

    01 模板 模板演示 配置路径 在 application 中配置模板文件和静态文件的路径: template_path='templates', static_path='static', 模板 & ...

  6. Python中的类(2)

    一.使用类和实例 我们先编写一个学生的类,它存储了有关学生的信息,还有一个整合学生信息的方法: student.py class Student(): def __init__(self,name,a ...

  7. LeetCode(75) Sort Colors

    题目 Given an array with n objects colored red, white or blue, sort them so that objects of the same c ...

  8. 关于程序计数器(PC)和条件控制转移 引起的性能差异

    关于PC(程序计数器) 冯 ·诺伊曼计算机体系结构的主要内容之一就是“程序预存储,计算机自动执行”! 处理器要执行的程序(指令序列)都是以二进制代码序列方式预存储在计算机的存储器中,处理器将这些代码逐 ...

  9. WCF部署到IIS的一个浅水滩

    俗话说,浅水淹死牛.昨天下午到今天上午,我就被淹死了一次. 最近在做毕业设计,和一个朋友做,做的是一个APP,我做的是服务器端,因为涉及后台数据更新,所以要有一个后台管理系统,然后还要搭建一个服务给A ...

  10. python3--产生偏移和元素:enumerate

    之前,我们讨论过通过range来产生字符串中元素的偏移值.而不是那些偏移值处的元素.不过,在有些程序中.我们两者都需要,要用的元素以及值个元素的偏移值.从传统意义来讲,这是简单的for循环,他同时也持 ...