Reactjs 如何修复&x201C;未捕获类型错误:回调不是函数”;由redux/redux thunk抛出?
我在redux操作中进行异步调用,当我分派成功的调用时,dev tools中返回以下错误:Reactjs 如何修复&x201C;未捕获类型错误:回调不是函数”;由redux/redux thunk抛出?,reactjs,typescript,react-native,react-redux,redux-thunk,Reactjs,Typescript,React Native,React Redux,Redux Thunk,我在redux操作中进行异步调用,当我分派成功的调用时,dev tools中返回以下错误: 未捕获类型错误:回调不是函数 当我注释掉分派调用时,应用程序当然会挂起,但我没有收到上面的错误。因此,这告诉我,我的redux操作可能存在一些缺陷 然后,错误中的其他行指向react native npm包中的行。我目前运行的是react native0.59.6。也许我正在运行的版本在这里有一个剧本 location.tsx const getLocationSuccess = (data: any)
未捕获类型错误:回调不是函数
当我注释掉分派调用时,应用程序当然会挂起,但我没有收到上面的错误。因此,这告诉我,我的redux操作可能存在一些缺陷
然后,错误中的其他行指向react native npm包中的行。我目前运行的是react native0.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)
})
}
}
getLocationSuccess
似乎没有在任何地方定义。从外观上看,如果您想使用提供给getLocation
的回调,您需要像这样调用dispatchdispatch(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)
})
}
}
export const getLoaction=(getLocationSuccess:Function)=>{
return(dispatch:Function,getState:Function)=>{
const{locationState}=getState();
常数{纬度,经度}=locationState.region;
axios.get(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${latitude},${longitude}&key=`)
。然后(json=>{
const address=json.data.results[0]。格式化的\u地址;
常数数据={
加载:false,
当前位置:地址
};
调度(getLocationSuccess(数据));
})
.catch((错误:任意)=>{
console.log('NAVIGATOR失败:',错误)
})
}
}
在函数名getlocationSuccess
中,字母L
很小。然而,这是资本,而作出呼吁 我解决了这个问题。问题不在任何redux文件中。这是React的版本问题。我没有在最新版本的React上运行,因此,React没有显式安装正确版本的“scheduler package 0.14.0”(错误消息指向该版本)
修复后,您可以安装正确的计划程序包,也可以升级到React的最新版本
他们在本期关于钩子的文章中提到:
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)
})
}
}