博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Redux] Refactoring the Entry Point
阅读量:5286 次
发布时间:2019-06-14

本文共 2419 字,大约阅读时间需要 8 分钟。

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(  
, 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.jsimport '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(  
, 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.jsimport { 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.jsimport React from 'react';import {Provider} from 'react-redux';import App from './App';const Root = ({ store }) => (    
)export default Root;

 

转载于:https://www.cnblogs.com/Answer1215/p/5558044.html

你可能感兴趣的文章
redis与spring结合错误情况
查看>>
第六章 字节码执行方式--解释执行和JIT
查看>>
字符串方法title()、istitle()
查看>>
yield语句
查看>>
查看linux系统中占用cpu最高的语句
查看>>
[洛谷P1738]洛谷的文件夹
查看>>
ubuntu server设置时区和更新时间
查看>>
【京东咚咚架构演进】-- 好文收藏
查看>>
【HTML】网页中如何让DIV在网页滚动到特定位置时出现
查看>>
文件序列化
查看>>
jQuery之end()和pushStack()
查看>>
Bootstrap--响应式导航条布局
查看>>
Learning Python 009 dict(字典)和 set
查看>>
JavaScript中随着鼠标拖拽而移动的块
查看>>
HDU 1021 一道水题
查看>>
The operation couldn’t be completed. (LaunchServicesError error 0.)
查看>>
php每天一题:strlen()与mb_strlen()的作用分别是什么
查看>>
工作中收集JSCRIPT代码之(下拉框篇)
查看>>
《转载》POI导出excel日期格式
查看>>
code异常处理
查看>>