react custom hooks vs normal functions, what is the difference

ReactjsReact Hooks

Reactjs Problem Overview


I'm trying to wrap my head around custom hooks. I understand normal hooks which is just fine, but my question is, when writing a custom hook, what is the difference between that and a normal function? I mean why not call it a normal function instead of calling it something like use*

Reactjs Solutions


Solution 1 - Reactjs

I believe no one has answered your question exactly. I'm still also understanding the benefits and purpose of having this extra feature called hooks, but I can still share my understanding.

React Hooks are JS functions with the power of react, it means that you can add some logic that you could also add into a normal JS function, but also you will be able to use the native hooks like useState, useEffect, etc, to power up that logic, to add it state, or add it side effects, memoization or more. So I believe hooks are a really good thing to manage the logic of the components in a isolated way.

So, you could have a foo.component.js (UI), a useFoo.js(logic), where useFoo will contain maybe many js functions and one hook to manage that functions and return what it's supposed.

This is an amazing video about react hooks, fully recommended

https://youtu.be/J-g9ZJha8FE

Solution 2 - Reactjs

There are some differences and problems that make us use react custom hooks:

  1. First of all, if you use normal functions, with every re-render of the component, this function will be created again and it causes the lack of performance. you may think that it can be fixed by using useCallBack and make react not to create a new function every time it re-renders but it is not the main problem that we are going to solve.
  2. The main problem as the react document has discussed with a brief example of tracking online friends, is avoiding copy-pasting the same logic in different functional components which they need to be stateful too.
  3. If we use normal functions inside a component and use useCallBack to avoid creating a new function every time, we did not solve the problem because in every component we should also copy this logic so this did not solve the problem.
  4. The other solution is to make a function outside the functional component to handle this logic, but there is a big problem: in a normal function outside of the component, we don't have access to states because as we mentioned, this implemented logic is stateful and we have access to states only in react components.

So what is the solution here? yes, Custom React Hooks!

  1. It is a stateful function that uses other react built-in hooks (e.g. useState, useCallback etc.) that can wrap around the stateful logic that you wanted to gather in one place and avoid copy and pasting the same logic in multiple components.
  2. With this approach, you can put your logic outside of the component in another function while you are getting the benefit of stateful functionalities of react.

I hope that this answer may solve your problem and resolve your ambiguity.

Solution 3 - Reactjs

React Hooks (custom or non-custom) should start with the use prefix. As well as, as per the React Documentation:

  1. Hooks should be called from the React code only not from the Regular JS functions. Hence, Hooks' scope is limited to the React code world and has more power to do a lot with React code. Rather than JS, regular functions could be used across application but as react code guidelines keep the code more aligned to react syntax.

  2. In the class-based components, the Hooks won't work but regular functions will.

  3. In the regular JS functions, you can't access useState, useEffect, useContext etc. but in react custom hooks I can.

Solution 4 - Reactjs

From the React docs: > A custom Hook is a JavaScript function whose name starts with ”use” and that may call other Hooks. [...] Its name should always start with "use" so that you can tell at a glance that the rules of Hooks apply to it.

So why define custom Hooks with a special "use" naming prefix?

1.) It tells consumers, that these functions are intended to be used with React and obey to an implicit contract (above mentioned rules).

2.) You can have tooling support which checks and enforces these rules. For example, eslint-plugin-react-hooks utilizes a heuristic that assumes, a function starting with "use" prefix and a capital letter after it is a Hook.

Solution 5 - Reactjs

A custom hook depends on one more other hooks. By design React hooks are meant to be used from a component's render method. You will get a warning if you try to use a hook anywhere else. Custom hooks follow the same convention as built-in hooks because they have to be used in the same fashion. The use prefix is just a convention to identify hook functions which are usually call at the very top of a component render method.

You can name your hook function anything you want but as I mentioned, you will get a warning from React if used outside of a render method.

Solution 6 - Reactjs

You can call it whatever and it will still work fine. The only advantage is that if you use the “useName” convention, React will check for error whether it correctly follows the rules of hooks. It just makes your task slightly easier.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionpandith padayaView Question on Stackoverflow
Solution 1 - ReactjsRodrigoView Answer on Stackoverflow
Solution 2 - ReactjsMohammad Mahdi MohajerView Answer on Stackoverflow
Solution 3 - ReactjsNeha SharmaView Answer on Stackoverflow
Solution 4 - Reactjsford04View Answer on Stackoverflow
Solution 5 - ReactjsPabloView Answer on Stackoverflow
Solution 6 - Reactjsah-marView Answer on Stackoverflow