Technoarch Softwares - Building Your Own Hooks

Building Your Own Hooks

What are hooks?
Hooks are same as Javascript functions. It allows you to use state and other React features without writing a class. Hooks are the functions which "hook into" react state and lifecycle features from function components. It does not work inside classes.

What are custom hooks?
Custom Hooks are method to reuse stateful logics in other components without writing extra code.Like we need a function which has to be used in many components. So instead of defining that function again and again, we will develop our own hook.

Rules of creating customs hooks :-

Whenever we will create a hook, first of all we have to keep in mind that the name of our hook must start with this word “use” and then its name shall begin. Like “useTestHook” and “useCustomHook”

So let's understand with an example

How to create a custom hook :-

In this image above we have developed a custom hook to change the title when the user clicks on the button. So basically, we are developing a function whose name is "useDocumentTitle" and giving it a parameter which is title, inside the hook we are using “useEffect” hook to change our page title when the title variable is changed.

When the user clicks on the Increment button then the title and the number will increase to 1.

Output :- 

Conclusion:-

Custom Hooks offer the flexibility of sharing logic that wasn’t possible in React components before. You can write custom Hooks that cover a wide range of use cases like form handling, animation, declarative subscriptions, timers, and probably many more we haven’t considered. What’s more, you can build Hooks that are just as easy to use as React’s built-in features.

Try to resist adding abstraction too early. Now that function components can do more, it’s likely that the average function component in your codebase will become longer. This is normal — don’t feel like you have to immediately split it into Hooks. But we also encourage you to start spotting cases where a custom Hook could hide complex logic behind a simple interface, or help untangle a messy component.

87 Comments:

  1. Leave a Reply

    Your email address will not be published. Required fields are marked *

  2. Leave a Reply

    Your email address will not be published. Required fields are marked *

  3. Leave a Reply

    Your email address will not be published. Required fields are marked *

  4. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  5. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  6. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  7. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  8. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  9. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  10. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  11. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  12. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  13. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  14. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  15. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  16. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  17. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  18. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  19. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  20. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  21. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  22. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  23. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  24. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  25. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  26. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  27. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  28. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  29. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  30. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  31. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  32. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  33. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  34. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  35. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  36. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  37. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  38. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  39. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  40. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  41. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  42. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  43. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  44. @@vabqd

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  45. 1'"

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  46. 1'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  47. 1*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  48. OZJ9JAy8')) OR 612=(SELECT 612 FROM PG_SLEEP(15))--

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  49. RszhNMnB') OR 631=(SELECT 631 FROM PG_SLEEP(15))--

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  50. 3AQApShV' OR 848=(SELECT 848 FROM PG_SLEEP(15))--

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  51. -1)) OR 977=(SELECT 977 FROM PG_SLEEP(15))--

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  52. -5) OR 451=(SELECT 451 FROM PG_SLEEP(15))--

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  53. -5 OR 825=(SELECT 825 FROM PG_SLEEP(15))--

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  54. GQKE4Ru8'; waitfor delay '0:0:15' --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  55. 1 waitfor delay '0:0:15' --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  56. -1); waitfor delay '0:0:15' --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  57. -1; waitfor delay '0:0:15' --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  58. (select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  59. 0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  60. 0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  61. if(now()=sysdate(),sleep(15),0)

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  62. -1" OR 3*2>(0+5+845-845) --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  63. -1" OR 3*2<(0+5+845-845) --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  64. -1" OR 3+845-845-1=0+0+0+1 --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  65. -1" OR 2+845-845-1=0+0+0+1 --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  66. -1' OR 3*2>(0+5+833-833) or 'zRIlRRJv'='

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  67. -1' OR 3*2<(0+5+833-833) or 'zRIlRRJv'='

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  68. -1' OR 3+833-833-1=0+0+0+1 or 'zRIlRRJv'='

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  69. -1' OR 2+833-833-1=0+0+0+1 or 'zRIlRRJv'='

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  70. -1' OR 3*2>(0+5+855-855) --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  71. -1' OR 3*2<(0+5+855-855) --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  72. -1' OR 3+855-855-1=0+0+0+1 --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  73. -1' OR 2+855-855-1=0+0+0+1 --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  74. -1 OR 3*2>(0+5+425-425)

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  75. -1 OR 3*2<(0+5+425-425)

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  76. -1 OR 3+425-425-1=0+0+0+1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  77. -1 OR 2+425-425-1=0+0+0+1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  78. -1 OR 3*2>(0+5+854-854) --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  79. -1 OR 3*2<(0+5+854-854) --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  80. -1 OR 3+854-854-1=0+0+0+1 --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  81. -1 OR 2+854-854-1=0+0+0+1 --

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  82. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  83. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  84. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  85. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  86. 1

    Leave a Reply

    Your email address will not be published. Required fields are marked *

Leave a Comments

Your email address will not be published. Required fields are marked *