如何解决为什么我的减速器没有在组件中显示它的值?
我有一个使用 Redux
的 React.js 应用程序。我有一个动作应该在减速器的状态下设置一个值,但它似乎不起作用。我使用 console.log()
在 reducer 中显示操作的 action.payload
并且它似乎已收到,但是当我在需要它的组件中调用 reducer 时,它没有显示。
这是我的代码:
操作:
export const setModeWarning = (warning) => ({
type: 'MQTT:SET_MODE_WARNING',payload: warning
});
减速器:
case 'MQTT:SET_MODE_WARNING':
console.log("ENTERED")
console.log(action.payload)
console.log(initialState.modeWarning)
return {
...state,modeWarning:action.payload.warning,}
组件:
const {modeWarning} = this.props
console.log("MODE WARNING")
console.log(modeWarning)
const mapStateToProps = (state) => {
return {
reconnect: state.reconnect,battery : state.battery,mode: state.mode,modeMessage:state.modeMessage,messageWarning:state.messageWarning,updateMessage:state.updateMessage,modeWarning:state.modeWarning
}
}
export default connect(mapStateToProps,null)(InformationScreen)
解决方法
确保操作有效载荷与减速器有效载荷相匹配
动作payload: warning
减速器modeWarning: action.payload // no .warning
或
动作payload: { warning }
减速器modeWarning: action.payload.warning
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。