Javascript 如何从传递有效负载的组件设置Redux中的状态?
我无法使用mapDispatchToProps设置状态并传递有效负载 进口:Javascript 如何从传递有效负载的组件设置Redux中的状态?,javascript,reactjs,redux,axios,Javascript,Reactjs,Redux,Axios,我无法使用mapDispatchToProps设置状态并传递有效负载 进口: import React, { Component } from 'react'; import classes from './HoursWatched.mod ule.css'; import axios from 'axios'; import { connect } from 'react-redux'; import * as actionType from '../../../store/actions';
import React, { Component } from 'react';
import classes from './HoursWatched.mod ule.css';
import axios from 'axios';
import { connect } from 'react-redux';
import * as actionType from '../../../store/actions';
这就是我试图触发动作的方式:
componentDidMount() {
this.getHoursWatched()
}
getHoursWatched() {
axios.get(API_URL, header)
.then((res) => {
console.log(res)
this.props.hoursWatched(res)
})
.catch((err) => {
console.log("Error", err)
})
}
地图显示为:
const mapDispatchToProps = dispatch => {
return {
hoursWatched : (res) => dispatch({ type: actionType.HOURS_WATCHED, payload: res })
}
}
出口:
export default connect(mapStateToProps, mapDispatchToProps)(HoursWatched);
在减速器中:
const reducer = (state = initialState, action, payload) => {
switch (action.type) {
case actionTypes.HOURS_WATCHED:
const newHoursWatched = {};
console.log(payload) //This is undefined
return {
...state,
hoursWatched: newHoursWatched
};
为什么我不能通过有效载荷?它说它是未定义的您试图引用减速器中的错误参数。为了访问从API响应传递的有效负载,您需要在减速机中使用
操作。有效负载
,而不是有效负载
,只需检查“Line 38:80”,并确保名称拼写正确。因为它看起来不像是hoursweated
vshoursweated