We will learn how to better separate the code in the entry point to prepare it for adding the router.

Currently, in the index.js, we configure the store and bootstrap our App component:

import 'babel-polyfill';
import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import throttle from 'lodash/throttle';
import todoApp from './reducers';
import App from './components/App';
import { loadState, saveState } from './localStorage'; const persistedState = loadState();
const store = createStore(
todoApp,
persistedState
); store.subscribe(throttle(() => {
saveState({
todos: store.getState().todos,
});
}, 1000)); render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);

I'm extracting the logic necessary to create the store, and to subscribe to it to persist the state into a separate file.

I'm calling this file configure store, and so I'm creating a function called configure store that is going to contain this logic so that the app doesn't have to know exactly how the store is created and if we have any subscribe handlers on it. It can just use the return store in the index.js file.

//index.js

import 'babel-polyfill';
import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import configureStore from './configureStore';
import Root from './components/Root'; const store = configureStore(); render(
<Root store={store}/>,
document.getElementById('root')
);

Also extract the Provider from index.js and replace with a Root, so that later we can use react-router inside Root component:

//configureStore.js

import { createStore } from 'redux';
import todoApp from './reducers';
import {loadState, saveState} from './localStorge'
import throttle from 'lodash/throttle'; const configureStore = () => {
const persistedState = loadState();
const store = createStore(todoApp, persistedState);
console.log(store.getState()); store.subscribe( throttle(() => {
console.log(store.getState());
const {todos} = store.getState();
saveState({
todos
})
}, 1000)); return store;
} export default configureStore;
// components/Root.js

import React from 'react';
import {Provider} from 'react-redux';
import App from './App'; const Root = ({ store }) => (
<Provider store={store}>
<App />
</Provider>
) export default Root;

[Redux] Refactoring the Entry Point的更多相关文章

  1. webpack+react+redux+es6开发模式

    一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...

  2. angular开发者吐槽react+redux的复杂:“一个demo证明你的开发效率低下”

    曾经看到一篇文章,写的是jquery开发者吐槽angular的复杂.作为一个angular开发者,我来吐槽一下react+redux的复杂. 例子 为了让大家看得舒服,我用最简单的一个demo来展示r ...

  3. Redux教程1:环境搭建,初写Redux

    如果将React比喻成士兵的话,你的程序还需要一位将军,去管理士兵(的状态),而Redux恰好是一位好将军,简单高效: 相比起React的学习曲线,Redux的稍微平坦一些:本系列教程,将以" ...

  4. Redux教程2:链接React

    通过前面的教程,我们有了简单的环境,并且可以运行Redux的程序,也对 如何编写Redux示例 有了初步的印象: 掌握了 使用Redux控制状态转移 ,继而驱动 React 组件发生改变,这才是学习R ...

  5. webpack+react+redux+es6

    一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...

  6. 从零开始配置TypeScript + React + React-Router + Redux + Webpack开发环境

    转载请注明出处! 说在前面的话: 1.为什么不使用现成的脚手架?脚手架配置的东西太多太重了,一股脑全塞给你,我只想先用一些我能懂的库和插件,然后慢慢的添加其他的.而且自己从零开始配置也能学到更多的东西 ...

  7. react看这篇就够了(react+webpack+redux+reactRouter+sass)

    本帖将对一下内容进行分享: 1.webpack环境搭建: 2.如何使用react-router: 3.引入sass预编译: 4.react 性能优化方案: 5.redux结合react使用: 6.fe ...

  8. 使用 webpack + react + redux + es6 开发组件化前端项目

    因为最近在工作中尝试了 webpack.react.redux.es6 技术栈,所以总结出了一套 boilerplate,以便下次做项目时可以快速开始,并进行持续优化.对应的项目地址:webpack- ...

  9. react+redux+webpack+git技术栈

    一.git bash here mdkr cnpm init -y ls -a ls -l ls -la隐藏的也可查看 cat package.json 二.npm npm i webpack-dev ...

随机推荐

  1. Smarty实现HTML静态化页面

    <?phprequire_once("./config/config.php"); ob_start();$id=$_GET[id];$sql="select * ...

  2. ASP.NET 后台不识别ASPX中的控件

    请问后台不识别ASPX中的控件,怎么解决 这个程序是在网上下载的 C# code <asp:DataGrid runat="server" ID="dgList1& ...

  3. (转) 关于成为linux运维工程师需要掌握的技能

    曾经在一年多前写了一篇关于要成为linux运维需要掌握哪些技能和工具的贴子,然后不小心被好几个网友抄袭转发到其它网站上,当然有些是认识的,最后还很幸运地被某些热心的学习者把那段内容剪下来当作圣经般的参 ...

  4. Opencv实现的简易绘图工具

    第一次写博,还是个菜鸟.最近开始学习Opencv,试着写了个简易的绘图工具(目前只写了画线和橡皮擦部分,画其它图形还有待往里添加),也算是记录自己的学习之路. #include "stdaf ...

  5. 手写JS无缝滚动插件

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  6. windows相关小知识

    获得本机MAC1 快捷键win+R打开运行窗口, 输入cmd回车进入控制台2 输入ipconfig -all  找到本地连接中的物理地址 根据IP获得MAC方法:1 进入cmd控制台,执行:ping ...

  7. Linux 继续进阶

    http://www.cnblogs.com/uhasms/archive/2011/11/15/2250474.html http://www.cnblogs.com/alexyuyu/articl ...

  8. 转:synchronized和LOCK的实现原理---深入JVM锁机制

    JVM底层又是如何实现synchronized的? 目前在Java中存在两种锁机制:synchronized和Lock,Lock接口及其实现类是JDK5增加的内容,其作者是大名鼎鼎的并发专家Doug ...

  9. php使用domdocument读取xml文件

    使用domdocument读取xml文件需要用到以下几个方法和属性: 方法: 1:读取xml文件:load() 2:获取标签的对象数组:getElementByTagName() 3:对象数组的索引: ...

  10. 第1章(三)安装VS2015和Xamarin For VS

    原文 第1章(三)安装VS2015和Xamarin For VS 操作系统:Win7  64位(sp1或更高版本) 1.安装VS2015 VS2015简体中文企业版:vs2015.ent_chs.is ...