state.get 在尝试映射 react-redux 状态时不是函数

state.get is not a function when trying to map the react-redux State

在这里,我试图在我的组件中显示源,但我总是发现错误,因为 state.get 不是一个函数。

reducer.js

let initialState = Immutable.fromJS({sources: []});

export default function (state = initialState, action){
     switch(action.type){
         case GetSources:
             return state.merge({
                sources: action.sources
             });
        break;
     }
     return state;
}

Component.js

function mapStateToProps(state){
     return{
         sources: state.get('sources')
     }
}

allReducers.js

`const allReducers = combineReducers({
    sources:sources,
    news:news
})`

请试试这个:

function mapStateToProps(state){
     return{
         sources: state.sources.get('sources')
     }
}

请注意,如果您使用 combineReducers,由给定的 reducer 管理的状态只是整个 redux 状态的一部分。在这种情况下,要访问由给定 reducer 管理的状态切片,您必须使用注册给定 reducer 的键名(在您的情况下为 sources)。
根据 Redux combineReducers docs,reducer 由 combineReducers 返回:

calls every child reducer, and gathers their results into a single state object. The shape of the state object matches the keys of the passed reducers

在我的例子中,这是因为 chrome 中的本地应用程序状态持续存在,并且在我清理数据站点后,它可以正常工作。我想这是因为我本地的状态是常规 JS 对象,需要先将其删除。否则它会将状态转换回常规 JS 对象,即使您将其组合为不可变。