Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useAsyncFn change default useCallback #2066

Open
samsch opened this issue Aug 12, 2021 · 1 comment
Open

useAsyncFn change default useCallback #2066

samsch opened this issue Aug 12, 2021 · 1 comment

Comments

@samsch
Copy link

samsch commented Aug 12, 2021

The author of #1671 ran into the same issue and proposed requiring the deps argument, but I think it makes more sense to drop the default useCallback deps.

I currently have a use case where I ran into this with something like

const [state, fetch] = useAsyncFn(() => {
  return loadSomeData(arg1, arg2, arg3, arg4);
});

While I could add the args to the deps array, it's clearer code if I could leave the deps array out and the function isn't memoized at all.
As a hacky workaround, I'm passing null to deps which then "works", but isn't the clearest code.

I think changing the default deps to be undefined or null would be the best solution here. https://github.com/streamich/react-use/blob/master/src/useAsyncFn.ts#L38

Besides making the default case more clear, it also means that the [] needs to be done explicitly, and since the React team has mentioned that an empty deps array is sus, that seems like a win to me.

@JoeDuncko
Copy link

Hi! @react-hookz/web, the new library by one of react-use's former maintainers (background here and here) has a new implementation of useAsyncFn called useAsync that solves this issue.

For those interested, there's an official migration guide for migrating from react-use to @react-hookz/web.

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants