简体   繁体   English

React,Redux,Immutable-无法访问mapStateToProps中的JSON密钥

[英]React, Redux, Immutable - cannot access JSON key within mapStateToProps

Hopefully a really simple one (react newbie here!) but I cannot seem to access a specific key within the state property returned from a react/redux/immutable reducer. 希望这是一个非常简单的按钮(在这里反应新手!),但是我似乎无法访问由react / redux / immutable reducer返回的state属性内的特定键。

Consider the following where I wish to return the value of state.api.authenticated : 考虑以下情况,我希望返回state.api.authenticated的值:

function mapStateToProps(state) {
  console.log('DEBUG 1: ', JSON.stringify(state));
  console.log('DEBUG 2: ', JSON.stringify(state.api));
  console.log('DEBUG 3: ', JSON.stringify(state.api.authenticated));
  return {
    authenticated: state.api.authenticated
  };
}

This returns the following: 这将返回以下内容:

DEBUG 1:  {"modals":{},"routing":{"locationBeforeTransitions":null},"api":{"loading":false,"requests":{},"errors":{"last":null},"data":{"articles":[]},"authenticated":true},"articles":{"to_read_count":0},"form":{"signin":{"registeredFields".... (redacted for brevity)

DEBUG 2:  {"loading":false,"requests":{},"errors":{"last":null},"data":{"articles":[]},"authenticated":true}

DEBUG 3:  undefined

So clearly state.api.authenticated IS in the state object and yet I cannot access it! 显然, state对象中的state.api.authenticated IS是,但是我无法访问它!

Any advice much appreciated. 任何建议都非常感谢。


Edit 1: Reducer initial state definition: 编辑1:Reducer初始状态定义:

const initialState = Map({
  loading: false,
  requests: OrderedMap({}),
  errors: Map({
    last: null
  }),
  data: Map({
    articles: List()
  }),
  authenticated: false
});

Reducer setting value: 减速机设定值:

case AUTH_USER:
      return state.set('authenticated', true);

SOLUTION: ok, thanks to the comments I found state.api.get worked: 解决方案:好的,感谢我发现state.api.get起作用的注释:

const mapStateToProps = (state) => {
  return {
    authenticated: state.api.get('authenticated')
  };
};

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

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