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. iOS: 学习笔记实例, 用代码控制视图创建与切换

    1. 创建iOS, Single View Application.2. 修改YYViewController.m // // YYViewController.m // DynamicViewDem ...

  2. linux建立信任关系

    (1).切换到需要建立信任关系的用户(2).执行命令:ssh-keygen  -d,然后一直回车.该命令会在用户home目录下生成一个隐藏的.ssh目录.目录里面有两个文件:id_dsa.id_dsa ...

  3. 2016021801 - Java内存区域学习笔记

    根据<深入理解java虚拟机>学习归纳整理学习笔记 程序计数器 用途:当前线程的字节码文件的行号指示器.(当前机场负责控制飞机降落的空管员:当前线程表示当前机场, 所执行的字节码等同于被等 ...

  4. 2016多校联合训练contest4 1012Bubble Sort

    Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  5. norflash移植及uboot 保存环境变量实验

    一.实验环境 实验板:TQ2440开发板 SDRAM:64M norflash:EN29LV160AB(2M) nandflash:(256M) 二.移植 本文不详谈从smdk2410移植到TQ244 ...

  6. Quartz1.8.5例子(八)

    /* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...

  7. C++11的新特性lambda的小试牛刀RAII

    C/C++的资源是手动管理的 这导致程序员在申请资源时,最后用完了偶尔会忘记回收 C++语言的发明者倡导RAII,资源获取即初始化 使用对象来管理资源的生命周期,在超出作用域时,析构函数自动释放资源 ...

  8. 掌握C++基础

    以下是笔者在ubuntu系统下编译运行通过的一些反应c++基础特性的例子,包括类,结构体,构造函数,析构函数,函数的重载,this指针,类的继承,虚函数,函数的覆盖和隐藏等.(由于格式问题代码中的乱码 ...

  9. linux socket使用经验总结

    1.  scoket函数中PF_INET AF_INET区别 在UNIX系列中,PF_INET表示poxis, BSD系列用AF_INET 2.  in_addr_t inet_addr(const ...

  10. Lua常用的数据结构表示

    1.矩阵 Lua中有两种表示矩阵的方法,一是“数组的数组”.也就是说,table的每个元素是另一个table.例如,可以使用下面代码创建一个n行m列的矩阵:mt = {}          -- cr ...