[Redux] Refactoring the Entry Point

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;
复制代码

 

posted @   Zhentiw  阅读(283)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2015-06-04 [D3] 11. Basic D3 chart interactivity on(), select(this), classed(class, trueorfalse)
2015-06-04 [D3] 10. Creating Axes with D3
2015-06-04 [D3] 9. Scatter Plot
2015-06-04 [D3] 8. Margins
2015-06-04 [D3] 7. Quantitative Scales
点击右上角即可分享
微信分享提示