将函数传递给自定义反应钩子

问题描述 投票:0回答:2

之前,我使用的是这样的 graphql 查询。数据是从查询返回的,我使用

setShowFlatList
data
返回:

  const [loadUsers, { data }] = useUsersLazyQuery({
    onCompleted: () => {
      setShowFlatList(data);
    },
});

现在我正在创建一个自定义的 React 钩子,我在其中使用这个 graphql 查询。看起来像这样:

export const useLoadUsers = (onCompleted: any) => {
  const [usersQuery, { data }] = useUsersLazyQuery({
    onCompleted: () => {
      if(onCompleted){
        onCompleted();
      }
    },
    onError: onLoadUserError,
    fetchPolicy: 'network-only',
  });

const loadUsers = async (
phoneNumber: number,
) => {
  const data = await usersQuery({
    variables: {
      where: {
        OR: [
          { phoneNumber: newPhoneNumber }
        ],
      },
    },
    });
  return data;
};
return loadUsers;
};

但是我不知道如何将

setShowFlatList
函数传递给钩子的
onCompleted
,这样我仍然可以在钩子中使用
data
作为其参数。

javascript reactjs typescript react-native react-hooks
2个回答
3
投票

编辑:

onCompleted
还应该有一个参数
data
,这样你就可以使用该参数。

export const useLoadUsers = (onCompleted: any) => {
  const [usersQuery, { data }] = useUsersLazyQuery({
    onCompleted: (data) => {
      if(onCompleted){
        onCompleted(data);
      }
    },
    onError: onLoadUserError,
    fetchPolicy: 'network-only',
  });

const loadUsers = async (
phoneNumber: number,
) => {
  const data = await usersQuery({
    variables: {
      where: {
        OR: [
          { phoneNumber: newPhoneNumber }
        ],
      },
    },
    });
  return data;
};
return loadUsers;
};

0
投票
export const useEditLocationName = (callback) => {
  return (params) => async(favouritePlaceId, customisedName) => {
    const [updateFavouritePlace] = useUpdateFavouritePlaceMutation({
      onCompleted: () => {
        if(callback){
          callback(params)
        }
        Alert.alert('Name aktualisiert');
      },
      onError: () => {
        Alert.alert('Oops, etwas ist schiefgelaufen');
      },
    });
    updateFavouritePlace({
      variables: {
        favouritePlaceId: favouritePlaceId,
        input: { customisedName: customisedName },
      },
    });
    return null;
  }

const editLocationName = useEditLocationName(setShowFlatList);
...
  const handleSubmitForm = (
    values: FormValues,
  ) => {
    editLocationName(params)(route.params.favouritePlaceId, values.locationName)
  };


/*
export const useEditLocationName = (callback) => {
  const [updateFavouritePlace] = useUpdateFavouritePlaceMutation({
    onCompleted: () => {
      if(callback){
        callback()
      }
      Alert.alert('Name aktualisiert');
    },
    onError: () => {
      Alert.alert('Oops, etwas ist schiefgelaufen');
    },
  });

  const editLocationName = async (
    favouritePlaceId: number,
    customisedName: string,
  ) => {
    updateFavouritePlace({
      variables: {
        favouritePlaceId: favouritePlaceId,
        input: { customisedName: customisedName },
      },
    });
    return null;
  };

  return editLocationName;
};
*/
....



© www.soinside.com 2019 - 2024. All rights reserved.