简体   繁体   English

获取数据后如何正确调度数据?

[英]how can i dispatch data rigth after get data?

if i act onPress={() => kakaoLosing()如果我采取行动Press={() => kakaoLosing()

i want to get data(which is profile) from getProfile by using async await我想通过使用异步等待从 getProfile 获取数据(这是配置文件)

and right after dispatch data to KAKAOLOG_IN_REQUEST,在将数据发送到 KAKAOLOG_IN_REQUEST 之后,

this is my code这是我的代码

            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>

but if i use this code但如果我使用这段代码

this error occure发生此错误

        ReferenceError: Can't find variable: profile

how can i fix my code??我该如何修复我的代码?

You've closed over profile in the getProfile function scope, it isn't available in the useCallback hook callback.您已在getProfile function scope 中关闭了profile ,它在useCallback挂钩回调中不可用。 You then also don't call getProfile to make the request.然后,您也不要调用getProfile来提出请求。

I don't think there's a need for the useCallback hook.我认为不需要useCallback钩子。 Just declare a normal async function and await the response.只需声明一个正常的async function 并等待响应。 Now profile is in the same scope as the dispatch .现在profiledispatch位于相同的 scope 中。

const Vieww = ({}) => {
  const kakaoLosing = async () => {
    const profile = await getKakaoProfile();

    dispatch({
      type: KAKAOLOG_IN_REQUEST,
      data: profile
    });
  };

  return (
    <Button1 onPress={kakaoLosing} >
      <Label>
        profile
      </Label>
    </Button1>
    ...

You can do it this way if you want it in separate functions:如果你想在单独的函数中这样做,你可以这样做:

    const Vieww = ({}) => {
        const profile = async () => {
          const data = await getKakaoProfile();
          return data;
        }

        const kakaoLosng = () => {
          const dataToDispatch = profile();
          
          dispatch({
            type: KAKAOLOG_IN_REQUEST,
            data: dataToDispatch 
            })
        }
        

or even simpler:甚至更简单:

        const Vieww = ({}) => {
            const kakaoLosng = async () => {
              const dataToDispatch =  await getKakaoProfile();
              
              dispatch({
                type: KAKAOLOG_IN_REQUEST,
                data: dataToDispatch 
                })
            }

and on button you can just pass:在按钮上你可以通过:

return (
    <Button1 onPress={kakaoLosing}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM