[英]React Redux MapStateToProps
Why do I get undefined
returned when I access the state? 访问状态时为什么返回
undefined
返回值? I use the Redux DevTools and see the state correctly updated via an action but I just cannot access the state values for some reason. 我使用Redux DevTools并通过操作看到状态已正确更新,但由于某种原因我无法访问状态值。 I get this sort of object returned when I access
state.dog
which seems wrong: 当我访问
state.dog
时,我得到了返回的这种对象,这似乎是错误的:
ct {size: 1, _root: pt, __ownerID: undefined, __hash: undefined, __altered: false}
Here is my container code: 这是我的容器代码:
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);
Here is the dog reducer: 这是狗减径器:
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;
Seems like you are using immutable. 好像您使用的是不可变的。 state.dog is not a simple js array but a immutable map or list.
state.dog不是简单的js数组,而是不可变的映射或列表。 You can access it natively with state.dog.toObject().hasBarked.
您可以使用state.dog.toObject()。hasBarked本机访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.