[Redux] Supplying the Initial state

Source: Internet
Author: User

We'll learn how to start a Redux app with a previously persisted state, and how it merges with the initial state specifi Ed by the reducers.

The initial state of store was defined by the Rootreducers:

Const TODOAPP = combinereducers ({  todos,  visibilityfilter,});

And we use the ' Todoapp ' to create store:

Const STORE = createstore (Todoapp);

So the initial state should is default value of each reducer ' s state:

Const TODOS = (state = [], action) += (state = ' show_all ', action) = {...

If we want to show some persosted data as initial state, we can pass the persisted data as a second args to ' CreateStore () ' Function:

Const Persistedstate = {  todos: [    {      0,      "Redux",      false     }   = CreateStore (Todoapp, persistedstate);

So the rules is:

    • If there is persisted data want to display, use this second args, otherwise use ES6 default param
    • Once persisted data is passed in, it would overwrite the default params.

[Redux] Supplying the Initial state

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.