On my screen file, I run a graphql query and fetch data
. I use setShowFlatList
with the data
returned:
const [showFlatList, setShowFlatList] = useState(''); .... const [loadUsers, { data }] = useUsersLazyQuery({ onCompleted: () => { setShowFlatList(data); },});
Now, I am creating a custom react hook that I can call when I want to run the query. It looks like this:
export const useLoadUsers = (onCompleted: any) => { const [usersQuery, { data }] = useUsersLazyQuery({ onCompleted: () => { if(onCompleted){ onCompleted(data); //[usersQuery, { data }] } }, onError: onLoadUserError, fetchPolicy: 'network-only', });const loadUsers = async (phoneNumber: number,) => { const data = await usersQuery({ variables: { where: { OR: [ { phoneNumber: newPhoneNumber } ], }, }, }); return data;};return loadUsers;};
If I pass lets say setShowFlatList('true')
then it would work. However, I want to setShowFlatList(data)
and I don't know how to make that work since the data
is obtained within the hook itself.
From my screen file,I cannot pass setShowFlatList(data)
since data isn't defined there. Similarly, I can't just pass setShowFlatList
alone either. So, how can I do setShowFlatList(data)
within the hook then?