Skip to content

Instantly share code, notes, and snippets.

@bpander
Last active October 26, 2019 14:41
Show Gist options
  • Save bpander/3f4711882badc2a203bd5490c74bd2d6 to your computer and use it in GitHub Desktop.
Save bpander/3f4711882badc2a203bd5490c74bd2d6 to your computer and use it in GitHub Desktop.
const [data, setData] = useState();
useEffect(() => {
const load = async () => {
const r = await asyncFn(userId);
setData(r);
};
load();
});
useEffect(() => {
handleData(data);
}, [data]);
// usage
const userId = 'lkj';
const loaders = {
phones: useAsync(
useCallback(
() => method === ValidationMethod.TwoFactor && getUserPhones(userId),
[userId, method],
),
useCallback(result => {
if (result.error) {
props.navigation.navigate('somewhere else');
return;
}
}, [props.navigation]),
[], // optional initial value
),
};
const isLoading = Object.values(loaders).some(loader => loader.isLoading);
const userPhones = loaders.phones.value;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment