簡體   English   中英

React,Redux,Immutable-無法訪問mapStateToProps中的JSON密鑰

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

希望這是一個非常簡單的按鈕(在這里反應新手!),但是我似乎無法訪問由react / redux / immutable reducer返回的state屬性內的特定鍵。

考慮以下情況,我希望返回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
  };
}

這將返回以下內容:

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

顯然, state對象中的state.api.authenticated IS是,但是我無法訪問它!

任何建議都非常感謝。


編輯1:Reducer初始狀態定義:

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

減速機設定值:

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

解決方案:好的,感謝我發現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