如何修复redux / redux thunk抛出的“ Uncaught TypeError:回调不是函数”?

时间:2019-07-24 02:50:52

标签: reactjs typescript react-native react-redux redux-thunk

我在redux操作中进行异步调用,当我调度成功的调用时,在开发工具中会返回以下错误:

Uncaught TypeError: callback is not a function

当我注释掉调度调用时,应用程序当然会挂起,但是我没有收到上面的错误。所以这告诉我,我的redux动作可能存在某些缺陷。

然后,错误中的其他行指向react-native npm软件包中的行。我目前正在运行本机0.59.6。也许我正在运行的版本在这里有作用。

location.tsx

const getLocationSuccess = (data: any) => {
  return {
    type: GET_GEOLOCATION_SUCCESS,
    data
  }
}

export const getLocation = (cb: Function) => {

  return (dispatch: Function, getState: Function) => {

    const { locationState } = getState();
    const { latitude, longitude } = locationState.region;
    axios.get(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${latitude},${longitude}&key=<API KEY>`)
    .then(json => {

      const address = json.data.results[0].formatted_address;

      const data = {
        loading: false,
        currentLocation: address
      };

      // THIS LINE IS CAUSING THE ERROR
      dispatch(getLocationSuccess(data));
      cb(true)

    })
    .catch((err: any) => {
      console.log('NAVIGATOR FAILED: ', err)
    })
  }
}

index.tsx

componentDidMount(){

  this.props.actions.getLocation(() => {
      console.log('LOCATION SUCCESS')
   });
 };

3 个答案:

答案 0 :(得分:0)

getLocationSuccess似乎没有在任何地方定义。从外观上看,如果要使用提供给getLocation的回调,则需要像这样dispatch(cb(data));调用dispatch或将getLocation的参数名更改为{{1} }:

getLocationSuccess

答案 1 :(得分:0)

在函数名outer(x, scalar) 中,字母getlocationSuccess小。但是,打电话时是资本。

答案 2 :(得分:0)

我解决了这个问题。问题不在任何redux文件中。这是React的版本问题。我没有在最新版本的React上运行,因此,React没有显式安装“ Scheduler软件包0.14.0”的正确版本(错误消息所指向的地方)。

此修复程序,您可以安装正确的调度程序包,也可以升级到最新版本的React。

他们在本期有关钩子的文章中提到了这一点: https://github.com/facebook/react/issues/15647