WebDevSimplified / useful-custom-react-hooks

1.95k stars 687 forks source link

Return as Array #2

Closed jkutianski closed 3 years ago

jkutianski commented 3 years ago

https://github.com/WebDevSimplified/useful-custom-react-hooks/blob/f5dda2e69d51e5da52aa75f21c6e83610def5923/src/9-useAsync/useAsync.js#L22

If you've multiple times useAsync() you can't use destructuring assignment

WebDevSimplified commented 3 years ago

You can destructure this as { loading: otherLoadingName } and with an object you can get only the parts you need.

jkutianski commented 3 years ago

Yes, I know. Looks more common to return something more like useState()

WebDevSimplified commented 3 years ago

I think it is really more personal preference which return method is most familiar. I generally prefer to work with objects unless I know I will be using all values returned from the function.

jkutianski commented 3 years ago

I agree with you about the return preference, and I generally prefer to return Objects. I explain how instead of why. Sorry, my fault. The most common React hooks use arrays as returns. In this case, I think, it's more about destructuring preference and consistency between all the hooks. It is much clearer in useTimeOut(). useAsync() is more like an edge case because it returns 3 values and if you want to use only 2 values you have to use a [value ,, loading] and it is a bit more error-prone.

hooks/blob/f5dda2e69d51e5da52aa75f21c6e83610def5923/src/2-useTimeout/useTimeout.js#L29 In this case, this looks much more similar to useState() to me.

const [ setTimeout ] = useTimeout()

I forgot to tell you that I personally really appreciate what you do. You help me so much with your videos.

WebDevSimplified commented 3 years ago

I appreciate the explanation. I still plan to keep the hooks as they are so they match with what I showed in the video, but if you implement these hooks in your own project you are more then welcome to modify them to your needs.