[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;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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