Reusable React Custom Form Component

ReactReactBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this challenge you will be developing a reusable custom form aka submit form in React with functional component in which you can add as much input fields and buttons as you want by sending them as props. You can use that SINGLE component in your future projects too to have login form, registration form or a data creation/updation form for any CRUD functionality.

This form does not add input fields dynamically but it has capability to create multiple fields by sending them as props. So, if you have big project which can have multiple forms, you can create ONE SINGLE CUSTOM FORM COMPONENT and call that wherever you want.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL react(("`React`")) -.-> react/FundamentalsGroup(["`Fundamentals`"]) react(("`React`")) -.-> react/AdvancedConceptsGroup(["`Advanced Concepts`"]) react(("`React`")) -.-> react/StateManagementGroup(["`State Management`"]) react/FundamentalsGroup -.-> react/jsx("`JSX`") react/FundamentalsGroup -.-> react/components_props("`Components and Props`") react/AdvancedConceptsGroup -.-> react/hooks("`React Hooks`") react/StateManagementGroup -.-> react/use_state_reducer("`Using useState and useReducer`") subgraph Lab Skills react/jsx -.-> lab-67586{{"`Reusable React Custom Form Component`"}} react/components_props -.-> lab-67586{{"`Reusable React Custom Form Component`"}} react/hooks -.-> lab-67586{{"`Reusable React Custom Form Component`"}} react/use_state_reducer -.-> lab-67586{{"`Reusable React Custom Form Component`"}} end

Custom Form

To get started, open the editor. You can see the following files from your editor.

├── public
├── src
│   ├── components
│   │   └── CustomForm
│   │       ├── CustomForm.css
│   │       └── CustomForm.js
│   ├── App.js
│   ├── index.css
│   └── index.js
├── package-lock.json
└── package.json

Requirements

  • To install the project dependencies, use the following command:

    npm i
  • Please complete this challenge in the App.js file.

  • Use the useRef hook to create two ref objects, usernameRef and passwordRef. These refs will be used to access the values of the input fields.

  • handleLogin function: This function is called when the "Login" button is clicked. It logs the values of the username and password input fields to the console and displays an alert with the entered username and password.

  • handleRegister function: This function is called when the "Register" button is clicked. It logs the values of the username and password input fields to the console.

Example

Once you have completed the code, run it with the following command:

npm start

The finished result is as follows:

finished

Summary

Congratulations! You have completed the Custom Form. You can practice more challenge in LabEx to improve your skills.

Other React Tutorials you may like