Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6214

setState with a value from a different file

$
0
0

On my screen file, I run a graphql query and fetch data. I use setShowFlatListwith 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?


Viewing all articles
Browse latest Browse all 6214

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>