以前,我使用的是这样的graphql查询。数据是从查询中返回的,我使用setShowFlatList
并返回了data
:
const [loadUsers, { data }] = useUsersLazyQuery({
onCompleted: () => {
setShowFlatList(data);
},
});
现在,我在使用此graphql查询的地方创建了一个自定义react钩子。看起来像这样:
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
作为其参数。
答案 0 :(得分:2)
编辑:onCompleted
还应该有一个参数数据,因此您可以使用该参数。
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;
};
答案 1 :(得分: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;
};
*/
....