DEV Community

Cover image for How to make Your Own Hook
Jayant
Jayant

Posted on

2 2

How to make Your Own Hook

How to make Your Own Hook

What

React Hooks are simple JavaScript functions that we can use to isolate the reusable part from a functional component.

How To make Your Own Hook

If you are using the same Function again and again but with different Names, you can make a Hook So that you don’t have to write the same code over and over again.

Let’s Take the Example a Simple Input Form.


React JS

In this InputForm Component, we are using the handleChange Function Multiple Times with the same Name. So we can make a Hook For this Purpose.

Before Starting making a Hook we have to Think of what are the Re-useable Functions in Input Form.

So generally we have ,

1. onSubmit Function: called when the form is submitted

2. onChange Function: called when the form is changed

3. onReset Function: called inside of the OnSubmit Function

So what our Hook will do, it will takes the Initial Input [Empty string ], set the state using the Initial value , then it will return 3 things

  • state
  • handle Change
  • handle Submit

In the Form of an Array or Object an then we can use it the Component.

UseInputForm Hook

React Hooks

Code

React JS

Follow For More Such Videos 😊

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

πŸ‘₯ Ideal for solo developers, teams, and cross-company projects

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay