[Redux] Refactoring the Entry point

Source: Internet
Author: User

We'll learn how to better separate the "code in the" 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-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's going to contain T His logic so, the app doesn ' t had to know exactly how the store was created and if we had any subscribe handlers on I T. It can just use the return store in the Index.js file.

// Index.js  ' Babel-polyfill' react' react-dom' react-redux'./configurestore '  './components/root '= configurestore (); render (  <root store={store}/>,  document.getElementById (' root '));

Also extract the Provider from Index.js and replace with a Root, so this later we can use React-router inside Root compone Nt:

//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)); returnstore;} ExportdefaultConfigurestore;

// Components/root.js  ' react' React-redux './app '= ({store}) = (    <provider Store ={store}>        <app/>    </Provider>default Root;

[Redux] Refactoring the Entry point

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.