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. 《3+1团队》【Alpha】Scrum meeting 4

    项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 3+1团队 团队博客地址 https://home.cnblogs.com/u/3-1group ...

  2. Linux OOM-killer 内存不足时kill高内存进程的策略

    OOM_killer是Linux自我保护的方式,当内存不足时不至于出现太严重问题,有点壮士断腕的意味 在kernel 2.6,内存不足将唤醒oom_killer,挑出/proc/<pid> ...

  3. linux 05

    日期与时间命令:date.日历的命令:cal.计算器:bc  要使用quit退出 在命令行模式里执行命令时,会有两种主要情况: 一种是该命令会直接显示结果然后回到命令提示符等待下一个命令的输入 dat ...

  4. 条款26:尽可能延后变量定义式的出现时间(Postpone variable definitions as long as possible)

    NOTE : 1.尽可能延后变量定义式的出现时间.这样做可增加程序的清晰度并改善程序效率.

  5. logging ,re 模块

    一,复习 # random: random() randint() choice() sample() # 序列化:对象需要持久化存储或传送 对象 => 字符串 # json: 用于传输 # - ...

  6. 一张图表,人人都能建立自己的AARRR运营模型

    每次跟同行聊运营,聊用户,聊产品,最后都会回到AARRR模型上来,这个用户全生命周期模型概括了互联网产品运营的5个关键环节. 获客是运营的基础,促进用户活跃才能让产品有生命力,提升留存减少流失让用户规 ...

  7. angularjs ng-repeat下验证问题

    angularjs验证要求name唯一,repeat情况,name 等通过${index}等绑定,也无法获取值 通过ng-from的方法,这样验证name重复也可以了. <ng-form nam ...

  8. Go map例题

    package main import "fmt" //map例题 //寻找最长不含有重复字符的子串 // abcabcbb -> abc //pwwkew ->wke ...

  9. 【记录】新建Cordova项目出现ios-deploy找不到的问题

    按老流程 Cordova create helloApp Cordova platform add ios 之前一般这种操作之后就能有执行的iOS目录了,像这样   然后 Cordova build ...

  10. BZOJ 1123 tarjan

    题目链接 题意:一张无向图,把第$i$个点关联的所有边去掉,求无向图中有多少个点对不连通. 题解: 如果割的不是割点,那么总答案是$2\times (n-1)$. 如果是割点,要分别考虑每个子树的贡献 ...