react-mbformhook
v1.0.3
Published
`react-mbformhook` is a simple, user-friendly React hook designed to handle form input data dynamically. It allows you to easily manage form state and automatically binds form fields based on their `name` attribute. This hook is perfect for forms of any s
Downloads
15
Readme
react-mbformhook
react-mbformhook is a simple, user-friendly React hook designed to handle form input data dynamically. It allows you to easily manage form state and automatically binds form fields based on their name attribute. This hook is perfect for forms of any size, as it eliminates the need to manually set up state for each form field.
Features
- Automatically manages form data for all fields.
- Simplifies form handling by updating form state dynamically.
- Easy to integrate with any form.
- Reduces boilerplate code for managing form state.
Installation
To use react-mbformhook, you need to install it via npm (or yarn):
Using npm:
npm install react-mbformhookUsing yarn:
yarn add react-mbformhookHow it Works
react-mbformhook provides a custom hook useForm that:
- Initializes the form state based on the initial data passed to it.
- Automatically updates the form state when an input field changes by using the field's
nameattribute. - Returns the form state and a change handler (
handleChange) to be used in your form inputs.
The hook uses useState to store and update the form data dynamically. You don't need to manually define the initial state for each form field — the hook will handle it for you.
Usage
Step-by-Step Guide
- Install the package using npm or yarn (as shown above).
- Import the
useFormhook fromreact-mbformhookin your component. - Use the hook in your form component to handle form state.
- Attach the
handleChangefunction to each form input element'sonChangeevent. - Submit the form and access the form data in the state.
Example
Here is a complete example of how to use react-mbformhook in a React component:
import React from 'react';
import { useForm } from 'react-mbformhook';
function App() {
// Step 2: Initialize the form data and change handler using the useForm hook
const [formData, handleChange] = useForm();
// Step 4: Handle form submission
const handleSubmit = (e) => {
e.preventDefault();
console.log("Form Submitted:", formData);
console.log("First Name:", formData.firstname); {/*name="firstname"*/}
};
return (
<div>
<h1>React Form Example with react-mbformhook</h1>
<form onSubmit={handleSubmit}>
{/* Step 3: Use handleChange for each input field */}
<input
type="text"
name="firstname"
onChange={handleChange}
placeholder="First Name"
/>
<input
type="text"
name="lastname"
onChange={handleChange}
placeholder="Last Name"
/>
<input
type="email"
name="email"
onChange={handleChange}
placeholder="Email"
/>
<button type="submit">Submit</button>
</form>
<h2>Form Data</h2>
<pre>{JSON.stringify(formData, null, 2)}</pre>
</div>
);
}
export default App;Breakdown of the Example
Initialization (
useForm):- The
useFormhook is used to initialize form state and handle changes. It returns two values:formData: The current state of the form data (an object).handleChange: A function to handle changes to input fields.
- The
Input Fields:
- Each input field uses
nameattributes (firstname,lastname,email) which correspond to keys in theformDatastate. - The
onChangehandler for each field is bound to thehandleChangefunction returned by theuseFormhook. - As you type in the fields,
handleChangeautomatically updates theformDatastate.
- Each input field uses
Form Submission:
- When the form is submitted, the
handleSubmitfunction is called. - It prevents the default form submission behavior using
e.preventDefault()and logs theformDatato the console.
- When the form is submitted, the
Displaying Form Data:
- The form data is displayed in a
pretag to show the JSON representation of the current form state.
- The form data is displayed in a
Dynamic Form Fields
If you need to handle a large form with many fields or dynamically add fields, you don't need to modify the hook or the code for each new field. Simply add the input fields with appropriate name attributes, and the useForm hook will handle them automatically.
API Reference
useForm(initialFormData)
Arguments:
initialFormData(optional): An object representing the initial values for the form fields. The object keys should correspond to the field names.
Returns:
- An array with two elements:
formData: The current form data (an object).handleChange: A function that updates theformDatawhen an input field changes.
License
MIT
### Key Points in the README:
- **Installation**: Step-by-step guide on how to install the package using npm or yarn.
- **How it Works**: Explanation of how the `useForm` hook works internally.
- **Usage**: Detailed instructions and a code example of how to use the hook in a React component.
- **Dynamic Form Fields**: Example of handling dynamic fields in a form.
- **API Reference**: Provides a description of the `useForm` hook, its arguments, and return values.
