Javascript 获取数据后,如何发送数据格式?
如果我按press={()=>kakaoLosing() 我想通过使用async await从getProfile获取数据(即概要文件) 在发送数据到KAKAOLOG_请求后 这是我的密码Javascript 获取数据后,如何发送数据格式?,javascript,node.js,reactjs,react-native,Javascript,Node.js,Reactjs,React Native,如果我按press={()=>kakaoLosing() 我想通过使用async await从getProfile获取数据(即概要文件) 在发送数据到KAKAOLOG_请求后 这是我的密码 import { getProfile as getKakaoProfile, } from '@react-native-seoul/kakao-login'; const Vieww = ({}) =>
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>
如何修复我的代码???您已经关闭了
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>
...
constvieww=({})=>{
const kakaoLosing=async()=>{
const profile=等待getKakaoProfile();
派遣({
类型:KAKAOLOG_IN_请求,
数据:个人资料
});
};
返回(
轮廓
...
如果您想在单独的功能中使用它,可以这样做:
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}>
返回(
为什么要用useCallback来代替普通的arrow函数?有什么用吗?你已经关闭了getProfile
函数范围中的profile
,它在useCallback
钩子回调中不可用。然后你也不会调用getProfile
来发出请求。我看不到async
>请查看我的答案,也许它会帮助你:)比你!!!!!!!
return (
<Button1 onPress={kakaoLosing}>