简体   繁体   English

React Reducer返回未定义

[英]React Reducer is returning undefined

I have multiple properties already in mapStateToProps(state) but this particular bit is returning undefined - the disableselectors 我在mapStateToProps(state)中已经有多个属性,但是此特定位返回未定义-disableselectors

return {
    token: state.token,
    url: state.url,
    disableselectors: state.disableselectors
}

so here is my reducer, I'm already returning a default state (same as my other reducers) 所以这是我的减速器,我已经返回了默认状态(与其他减速器相同)

const initState = "ENABLED"; 

export default function disableSelectorReducer(state = initState, action){
    switch(action.type){
        case 'SET_DISABLEDSELECTORS':
            return action.disableselectors;

        default:
            return state;
    }
}

when I try to access it on my component it returns undefined 当我尝试在组件上访问它时,它返回未定义

doUpdate(){
let x = this.props.disableselectors;
}

Component where I set the state and action 我设置状态和动作的组件

 const disableselectors = this.state.disableselectors;
        disableselectors.disableSelectorProperty = "DISABLED";
        this.setState({disableselectors});
        this.props.setDisableSelectors(this.state.disableselectors.disableSelectorProperty);

root reducer 根减速机

const rootReducer = combineReducers({
    status,
    disableSelectors
})

export default rootReducer;

are there any issue for this? 这有什么问题吗? since my other props and reducers are working fine 因为我的其他道具和减速器工作正常

With combineReducers , the property names that are passed in become the properties in the combined state. 使用combineReducers ,传入的属性名称将变为合并状态下的属性。 Because your combineReducers looks like this: 因为您的combineReducers看起来像这样:

const rootReducer = combineReducers({
  status,
  disableSelectors
})

...your mapping of state to props ...您将国家映射到道具

return {
  token: state.token,
  url: state.url,
  disableselectors: state.disableselectors
};

...is incorrect since the state you're looking for is under a disableSelectors property, not a disableselectors property (notice the capitalization you're using doesn't match the capitalization in your combineReducers ). ...不正确,因为你正在寻找的状态是下disableSelectors属性,而不是disableselectors属性(注意你使用的是不匹配的资本资本combineReducers )。 Either update your mapping function to access disableSelectors or update your combineReducers so the property is disableselectors . 更新您的映射功能访问disableSelectors或更新combineReducers这样的属性是disableselectors

(personally I'd recommend always using camelCase to avoid this sort of issue). (我个人建议始终使用camelCase以避免此类问题)。

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

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