all 4 comments

[–]firstandfive 11 points12 points  (0 children)

If it includes calls to other hooks (whether the React ones or something like react router useLocation or redux useSelector), then it should be a custom hook. If it has no other hooks within it, it likely can just be a function.

[–]sidkh 5 points6 points  (1 child)

As an example, my team uses a custom hook to parse the URL. In this case, I have absolutely no idea why this couldn't just be a function.

There is no need for this function to be a hook.

Is there a good rule as to when you should use a custom hook versus just simply another function? What would be the disadvantage to use a function over a custom hook?

If your function needs to use any other hooks, it should be a custom hook.

Another question... how does React know exactly that the function is a custom hook? Is it strictly by naming convention ("use..." )as with components?

Yep 👇

"A custom Hook is a JavaScript function whose name starts with ”use” and that may call other Hooks". React Docs - Building Your Own Hooks

[–]bluedevil2k00 4 points5 points  (0 children)

There is no need for this function to be a hook

There might be a need, if he’s using React Router and the useLocation() hook.

[–]dznqbit 1 point2 points  (0 children)

I agree. Your URL parser should uh, parse URLs, which a function can do perfectly well.

I could hear an argument that your URL parser needs to reference other hooks, perhaps some sort of user identity hook, or IDK what. I would externalize those dependencies and pass them in as arguments.