简体   繁体   English

redux - reducer状态为空白

[英]redux - reducer state is blank

I am trying to replicate something similar to the TodoList example in the the redux docs' basic example . 我试图复制类似于redux docs基本示例中的TodoList 示例 The second reducer receives an array - styleItems = [{... ... }, {... ...}] - and then calls the first function to act on each of the individual objects. 第二个reducer接收一个数组 - styleItems = [{... ... }, {... ...}] - 然后调用第一个函数来处理每个单独的对象。

I provide an initialState to the app container via the following, as shown in containers/app.js . 我通过以下内容向app容器提供了一个initialState ,如containers/app.js所示。 However, the state passed to the styleItems reducer seems to be a blank array - each and every time. 但是,传递给styleItems reducer的状态似乎是一个空白数组 - 每次都是。

However, react renders the UI based on the initial config, and react dev-tools shows the state structure as expected. 但是,react会根据初始配置呈现UI,并且dev-tools会按预期显示状态结构。 Is the redux store somehow seeing the same thing as react? redux商店是否会以某种方式看到同样的反应?

containers/app.js 集装箱/ app.js

function starterInfo(state) {
    return {

        // The ID of this particular object
        id: 12345,

        // Various keys and theri css values
        styleItems: [
            {
                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'
            }
        ],

        // Network state info
        currently_fetching: false,
        currently_posting: false
    }
}

export default connect(starterInfo)(App)

reducers/index.js 减速器/ index.js

// This handles a single styleItem object within the array
function change_css(state = {}, action){
    switch (action.type){
        case actions.CHANGE_CSS:

            if (state.order !== action.order){
                return state
            }

            return {
                ...state,
                css_value
            }

        default:
            return state
    }
}

// This handles the styles array in the global state
function styleItems(state = [], action){
    switch(action.type){       
        case actions.CHANGE_CSS:

            const foobar = state.map(styleItem =>
                change_css(styleItem, action)
                )            

            return foobar

        default:
            return state
    }
}

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 . React Redux绑定的connect函数的第一个参数是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. 此功能的目的是获取应用程序中存在的状态并将其映射到组件的props。 What you're doing in your starterInfo function is kind of just hard-coding what the state is for your component. 你在starterInfo函数中所做的只是硬编码你的组件的状态。 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. 因为你正在返回一个普通对象,React并不真正知道它的区别,所以它工作得很好,但是Redux还不知道你的app状态。

Instead, what you should do is provide your initial state directly to the reducers, like this: 相反,你应该做的是直接向reducers提供你的初始状态,如下所示:

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. 最后,因为你要将你的combineReducers拆分起来,你需要再次使用Redux的combineReducers实用程序将它们组合在一起,将root reducer提供给你的商店并从那里开始。

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 您还可以使用redux函数createstore传递初始状态,该函数将参数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 如果您使用comibneReducer初始化您的状态

var reducer = combineReducers({
    css: change_css,
    items: styleItems
})

Now 现在

var store = createStore(reducer)
console.log(store.getState())

Your store will contain { css: {}, items: [] } 您的商店将包含{ 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函数的第二个参数传递。

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 例如,您可以从服务器提供此状态,并将其设置为应用程序的初始状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM