Skip to content

Instantly share code, notes, and snippets.

@icarus-sullivan
Created July 4, 2019 19:00
Show Gist options
  • Save icarus-sullivan/719b797fe1102f221f295e13c3f79125 to your computer and use it in GitHub Desktop.
Save icarus-sullivan/719b797fe1102f221f295e13c3f79125 to your computer and use it in GitHub Desktop.
HOC for useAsync
// ... useAysnc code above
export const withAsync = (fn, ...vargs]) => (Component) => ({ args, ...props }) => {
// If users override args in props, use those instead of vargs
const conditionalArgs = args || vargs;
// merge useAsync props with given props
const mergeProps = {
...props,
...useAsync(fn, ...conditionalArgs),
};
// inject all mergeProps
return (
<Component {...mergeProps} />
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment