score:7

Accepted answer

the short answer is that you're not passing the initial state quite right. the first argument to the connect function for the react redux bindings is mapstatetoprops. the point of this function is to take the state that already exists in your app and map it to props for your component. what you're doing in your starterinfo function is kind of just hard-coding what the state is for your component. because you're returning a plain object react doesn't really know the difference so it works just fine, but redux doesn't yet know about your app state.

instead, what you should do is provide your initial state directly to the reducers, like this:

const intialstyleitemsstate = [
    {
        pk: 31,
        order: 1,
        label: 'caption text color',
        css_identifier: '.caption-text',
        css_attribute: 'color',
        css_value: '#ffffff'
    },
    {
        pk:23,
        order: 2,
        label: 'caption link color',
        css_identifier: '.caption-link',
        css_attribute: 'color',
        css_value: '#fefefe'
    }
];

function styleitems(state = intialstyleitemsstate, action){ ...

and eventually, because you're splitting your reducers up you'll need to combine them back together again with redux's combinereducers utility, provide that root reducer to your store and go from there.

score:1

you can also pass the initial state using the redux function createstore that take as argument createstore(reducer, [initialstate]) http://rackt.org/redux/docs/api/createstore.html

let’s say you have two reducers

change_css(state = {}, action)
function styleitems(state = [], action)

if you use comibnereducer to initialize your state

var reducer = combinereducers({
    css: change_css,
    items: styleitems
})

now

var store = createstore(reducer)
console.log(store.getstate())

your store will contain { css: {}, items: [] }

now if you want to initialize the state you can pass the initial state as the second argument of the createstore function.

createstore(reducer, {css:{some properties},items:[{name:"obj1"},{name:"obj2"},{name:"obj3"}]})

now you store will contain the initial state. {css:{some properties,items:[{name:"obj1"},{name:"obj2"},{name:"obj3"}]}

you can feed this state from server for example and set it as initial state of your application


Related Query

More Query from same tag