Javascript 使用ComponentDidMount中的道具调用操作
我正在从componentDidMount调用一个操作,该操作在reducer上定义了一个属性,但是当它到达该操作时,参数没有定义 在我的组件中,我使用属性(this.props.selection)调用操作“fetchAppClasses”: 这是减速器返回的状态:Javascript 使用ComponentDidMount中的道具调用操作,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我正在从componentDidMount调用一个操作,该操作在reducer上定义了一个属性,但是当它到达该操作时,参数没有定义 在我的组件中,我使用属性(this.props.selection)调用操作“fetchAppClasses”: 这是减速器返回的状态: const selection = { timespan: "-3660", customTimespan: false, pathIds: [''], source: undefined, direction
const selection = {
timespan: "-3660",
customTimespan: false,
pathIds: [''],
source: undefined,
direction: 0,
appClassIds: []
};
在变量“selection”中,传递的参数应可用,但未定义:
export function fetchAppClasses(selection) {
return function (dispatch) {
var axcfg = { headers: { 'X-Auth-Token': window[config.storageType].token } };
return axios.get(config.apiUrl + '/sdwan/appClasses', axcfg)
.then(function (response) {
console.log('SDWanActions.fetchAppClasses response:', response);
dispatch(fetchAppClassesSuccess(response.data));
})
}
}
在
fetchAppClasses
函数中调用axios之前,请尝试删除return
export function fetchAppClasses(selection) {
return function(dispatch) {
var axcfg = {
headers: { 'X-Auth-Token': window[config.storageType].token },
};
// remove the return axios.get...
axios.get(config.apiUrl + '/sdwan/appClasses', axcfg)
.then(function(response) {
console.log(selection); // this should work now...
console.log('SDWanActions.fetchAppClasses response:', response);
dispatch(fetchAppClassesSuccess(response.data));
});
};
}
在
fetchAppClasses
函数中调用axios之前,请尝试删除return
export function fetchAppClasses(selection) {
return function(dispatch) {
var axcfg = {
headers: { 'X-Auth-Token': window[config.storageType].token },
};
// remove the return axios.get...
axios.get(config.apiUrl + '/sdwan/appClasses', axcfg)
.then(function(response) {
console.log(selection); // this should work now...
console.log('SDWanActions.fetchAppClasses response:', response);
dispatch(fetchAppClassesSuccess(response.data));
});
};
}
您是否使用
mapDispatchToProps
映射了fetchAppClasses
?如果没有-应该使用dispatch调用该操作。是的,我这样做了。更新代码。调用操作但不发送参数如果不工作,可以在fetchAppClasses
中尝试使用getState。类似于:returnfunction(dispatch,getState){const{selection}=getState().SDWanSelectionReducer}
I必须将函数参数中的选择传递给axios success。它正在到达功能,但在成功时丢失了,我需要它。你确定这不是比赛条件导致的问题吗?如果在componentDidMount调用之前未设置更新选择的减速器,则可能不会设置该值。如果您还提供了reducer代码,这可能会有所帮助。您是否将fetchAppClasses
映射到mapDispatchToProps
?如果没有-应该使用dispatch调用该操作。是的,我这样做了。更新代码。调用操作但不发送参数如果不工作,可以在fetchAppClasses
中尝试使用getState。类似于:returnfunction(dispatch,getState){const{selection}=getState().SDWanSelectionReducer}
I必须将函数参数中的选择传递给axios success。它正在到达功能,但在成功时丢失了,我需要它。你确定这不是比赛条件导致的问题吗?如果在componentDidMount调用之前未设置更新选择的减速器,则可能不会设置该值。如果您也提供了reducer代码,这可能会有所帮助。