react-async-hook icon indicating copy to clipboard operation
react-async-hook copied to clipboard

useAsyncCallback doesn't require a dependency array?

Open theogravity opened this issue 3 years ago • 1 comments

I'm trying to use this in place of react's useCallback(), and was wondering if a dependency array is required as I don't see one in the signature. Based on the usage examples, I get a sense that this isn't meant to replace it?

theogravity avatar Feb 15 '22 06:02 theogravity

No this is not like reacts useCallback. Instead you use useAsyncCallback when you want to control when the async operation happens. E.g. on a form submit.

you could do something like:

// where createUser is your async function that creates you a user
const createUserAsync = useAsyncCallback(createUser);
const onSubmit = (values) => createUserAsync.execute(values);

<form onSubmit={onSubmit}>
<input name="email" />
<input name="password" type="password" />
</form>

JoshWellPharma avatar Apr 01 '22 10:04 JoshWellPharma