Why is this.props.eventPassed logged as "undefined"?:(为什么this.props.eventPassed被记录为“未定义”?:)
The action function ( eventPassed
) you are trying to access at this point does not exist at this.props.actions.eventPassed
.
(此时您要访问的动作函数( eventPassed
)在this.props.actions.eventPassed
中不存在。)
It actually exists solely on this.props.actions
.(它实际上仅存在于this.props.actions
。)
This is because you bound the action method to the value ' actions ' in your mapDispatchToProps.
(这是因为您将action方法绑定到mapDispatchToProps中的值“ actions ”。)
This is turn provides gives access to the eventPassed
action via this.props.actions
.(依次提供通过this.props.actions
提供对eventPassed
动作的this.props.actions
。)
Since this.props.actions points to eventPassed
, by trying to access this.props.actions.eventPassed
you are trying to access the property of 'eventPassed' on the action eventPassed
.
(由于this.props.actions点eventPassed
,试图通过访问this.props.actions.eventPassed
您试图访问对行动“eventPassed”的财产eventPassed
。)
The result is that when you log for this property you receive " undefined "(结果是,当您登录此属性时,您会收到“ undefined ”)
Other necessary amendments:(其他必要的修正:)
mapDispatchToProps
needs to return a value
(mapDispatchToProps
需要返回一个值)
An arrow function with a block body does not automatically return a value and therefore one must be defined.
(具有块体的箭头功能 不会自动返回值 ,因此必须定义一个值 。)
So your function needs to look like:(因此,您的函数需要如下所示:)
mapDispatchToProps = (dispatch) => {
return { actions: bindActionCreators(eventPassed, dispatch) }
}
Initial state is an array containing an object:
(初始状态是一个包含对象的数组:)
const initialState = [{
eventPassed: false
}];
Since you're trying to reference it later as an object { eventPassed: true}
and not an array of objects [ { eventPassed: true } ]
it should be:
(由于您稍后尝试将其引用为object { eventPassed: true}
而不是对象数组[ { eventPassed: true } ]
因此应为:)
const initialState = {
eventPassed: false
};
The reducer needs to pass back the correct updated (but unmutated ) state:
(减速器需要传递正确的更新(但未突变 )状态:)
export default function eventPassed(state = initialState, action) {
switch (action.type) {
case actionTypes.EVENT_PASSED:
return {
...state,
eventPassed: action.payload
};
default:
return state;
}
}
What you were initially doing was returning a state that was no longer an object (or in the original case an array of object) but just the value of true
(您最初要做的是返回一个不再是对象(或在原始情况下为对象数组)的状态,而只是true
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…