Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ComponentDidMount中的道具调用操作_Javascript_Reactjs_Redux_React Redux - Fatal编程技术网

Javascript 使用ComponentDidMount中的道具调用操作

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

我正在从componentDidMount调用一个操作,该操作在reducer上定义了一个属性,但是当它到达该操作时,参数没有定义

在我的组件中,我使用属性(this.props.selection)调用操作“fetchAppClasses”:

这是减速器返回的状态:

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代码,这可能会有所帮助。