npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

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-mbformhook

Using yarn:

yarn add react-mbformhook

How 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 name attribute.
  • 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

  1. Install the package using npm or yarn (as shown above).
  2. Import the useForm hook from react-mbformhook in your component.
  3. Use the hook in your form component to handle form state.
  4. Attach the handleChange function to each form input element's onChange event.
  5. 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

  1. Initialization (useForm):

    • The useForm hook 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.
  2. Input Fields:

    • Each input field uses name attributes (firstname, lastname, email) which correspond to keys in the formData state.
    • The onChange handler for each field is bound to the handleChange function returned by the useForm hook.
    • As you type in the fields, handleChange automatically updates the formData state.
  3. Form Submission:

    • When the form is submitted, the handleSubmit function is called.
    • It prevents the default form submission behavior using e.preventDefault() and logs the formData to the console.
  4. Displaying Form Data:

    • The form data is displayed in a pre tag to show the JSON representation of the current form state.

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:
    1. formData: The current form data (an object).
    2. handleChange: A function that updates the formData when 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.