2016-10-29 7 views
0

상태에 액세스 할 때 undefined이 반환되는 이유는 무엇입니까? 나는 Redux DevTools를 사용하고 액션을 통해 올바르게 업데이트 된 상태를 확인하지만 어떤 이유로 든 상태 값에 액세스 할 수 없습니다.React Redux MapStateToProps

import * as Immutable from 'immutable'; 
import { MAKE_BARK } from '../actions/dog'; 

const initialState = Immutable.Map({ 
    hasBarked: false, 
}); 

const dogReducer = (state: Object = initialState, action: Object) => { 
    switch (action.type) { 
    case MAKE_BARK: 
     return state.set('hasBarked', action.payload); 
    default: 
     return state; 
    } 
}; 

export default dogReducer; 
+1

한 번 '개 감속기'표시 –

+0

감속기 코드를 질문에 추가하여 문제를 정확하게 지적하십시오. – NiFi

답변

2

는 것 같다 : 여기

import { connect } from 'react-redux'; 
import Message from '../../components/message'; 

const mapStateToProps = (state) => { 
    console.log(state.dog.hasBarked); 
    return { 
     message: state.dog.hasBarked ? 'Barked' : 'It is quiet', 
    }; 
}; 

export default connect(mapStateToProps)(Message); 

이 개 감속기입니다 : 여기
ct {size: 1, _root: pt, __ownerID: undefined, __hash: undefined, __altered: false} 

내 컨테이너 코드입니다 : 내가 잘못된 것 state.dog에 액세스 할 때 개체의 이런 종류의 반환받을 당신은 불변을 사용하고 있습니다. state.dog는 간단한 js 배열이 아니라 불변의 맵 또는리스트입니다. state.dog.toObject(). hasBarked를 사용하여 기본적으로 액세스 할 수 있습니다.

+0

고마움, 불변성이 사용되었습니다. – user3554382

관련 문제