如果我执行onPress={() => kakaoLosing()
我想使用异步等待从getProfile获取数据(即配置文件
在将数据发送到KAKAOLOG_IN_REQUEST之后,
这是我的代码
import {
getProfile as getKakaoProfile,
} from '@react-native-seoul/kakao-login';
const Vieww = ({}) => {
const kakaoLosing = useCallback(() => {
const getProfile = async () => {
const profile = await getKakaoProfile();
};
dispatch({
type:KAKAOLOG_IN_REQUEST,
data:profile
})
},[]);
return (
<Button1 onPress={() => kakaoLosing()} >
<Label>
profile
</Label>
</Button1>但是如果我用这个代码
这个错误发生了
ReferenceError: Can't find variable: profile如何修复我的代码??
发布于 2021-04-09 15:41:02
您已经在getProfile函数作用域中关闭了profile,它在useCallback钩子回调中不可用。然后,您也不需要调用getProfile来发出请求。
我不认为有必要使用useCallback钩子。只需声明一个普通的async函数并等待响应即可。现在,profile与dispatch在同一个作用域中。
const Vieww = ({}) => {
const kakaoLosing = async () => {
const profile = await getKakaoProfile();
dispatch({
type: KAKAOLOG_IN_REQUEST,
data: profile
});
};
return (
<Button1 onPress={kakaoLosing} >
<Label>
profile
</Label>
</Button1>
...发布于 2021-04-09 15:43:06
如果你想在不同的函数中实现,你可以这样做:
const Vieww = ({}) => {
const profile = async () => {
const data = await getKakaoProfile();
return data;
}
const kakaoLosng = () => {
const dataToDispatch = profile();
dispatch({
type: KAKAOLOG_IN_REQUEST,
data: dataToDispatch
})
}或者更简单:
const Vieww = ({}) => {
const kakaoLosng = async () => {
const dataToDispatch = await getKakaoProfile();
dispatch({
type: KAKAOLOG_IN_REQUEST,
data: dataToDispatch
})
}在按钮上,你可以直接传递:
return (
<Button1 onPress={kakaoLosing}>https://stackoverflow.com/questions/67016757
复制相似问题