How to fix “Uncaught TypeError: callback is not a function” thrown by redux/redux thunk?
How to fix “Uncaught TypeError: callback is not a function” thrown by redux/redux thunk?
我在我的 redux 操作中进行异步调用,当我发送一个成功的调用时,开发工具返回以下错误:
Uncaught TypeError: callback is not a function
当我注释掉调度调用时,应用程序当然会挂起,但我没有收到上述错误。所以这告诉我我的 redux 操作可能有一些缺陷。
然后错误中的其他行指向 react-native npm 包中的行。我目前 运行 使用 react-native 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')
});
};
getLocationSuccess
似乎没有在任何地方定义。从它的外观来看,如果你想使用提供给 getLocation
的回调,你需要像这样调用调度 dispatch(cb(data));
或将 getLocation
的参数名称更改为 getLocationSuccess
:
export const getLoaction = (getLocationSuccess: 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
};
dispatch(getLocationSuccess(data));
})
.catch((err: any) => {
console.log('NAVIGATOR FAILED: ', err)
})
}
}
在函数名 getlocationSuccess
中,字母 L
很小。然而,打电话时是资本。
我解决了这个问题。问题不在任何 redux 文件中。这是 React 的版本问题。我没有 运行 使用最新版本的 React,因此 React 没有明确安装 "scheduler package 0.14.0" 的正确版本(这是错误消息所指向的)。
修复,您可以安装正确的调度程序包,也可以升级到最新版本的 React。
他们在这个关于钩子的问题中提到了它:
https://github.com/facebook/react/issues/15647
我在我的 redux 操作中进行异步调用,当我发送一个成功的调用时,开发工具返回以下错误:
Uncaught TypeError: callback is not a function
当我注释掉调度调用时,应用程序当然会挂起,但我没有收到上述错误。所以这告诉我我的 redux 操作可能有一些缺陷。
然后错误中的其他行指向 react-native npm 包中的行。我目前 运行 使用 react-native 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')
});
};
getLocationSuccess
似乎没有在任何地方定义。从它的外观来看,如果你想使用提供给 getLocation
的回调,你需要像这样调用调度 dispatch(cb(data));
或将 getLocation
的参数名称更改为 getLocationSuccess
:
export const getLoaction = (getLocationSuccess: 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
};
dispatch(getLocationSuccess(data));
})
.catch((err: any) => {
console.log('NAVIGATOR FAILED: ', err)
})
}
}
在函数名 getlocationSuccess
中,字母 L
很小。然而,打电话时是资本。
我解决了这个问题。问题不在任何 redux 文件中。这是 React 的版本问题。我没有 运行 使用最新版本的 React,因此 React 没有明确安装 "scheduler package 0.14.0" 的正确版本(这是错误消息所指向的)。
修复,您可以安装正确的调度程序包,也可以升级到最新版本的 React。
他们在这个关于钩子的问题中提到了它: https://github.com/facebook/react/issues/15647