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 🙏

© 2024 – Pkg Stats / Ryan Hefner

useformbee

v1.0.3

Published

useFormBee is a custom react hook that helps handle form input, validations and other similar functionalities

Downloads

3

Readme

Build Status License: MIT PRs Welcome NPM Version

The problem

Handling forms in react can be a little bit tedious especially for new beginners like me 😃. I often find myself re-writing the same stateful logic and form validations, especially when I have different forms in different components (such as signup, login, profile update form).

Inspiration

My mentor @benfluleck suggested I abstract my form logic so that it can easily be reusable. I created a useForm hook and showed it to him and my other colleague. To my surprise, they loved it and adviced that I made some improvements on it and also upload it to npm as a library.

The solution

useFormBee is a custom react hook that helps me manage and abstract form logic. Form logic such as

  • values
  • onChange
  • onSubmit
  • onReset

Harnessing the power of validatorjs, I integrated validations to the useForm hooks.

Installation

This module is distributed via npm

npm install useformbee

Usage

Import
import useFormBee from 'useformbee';
const { values, errors, handleChange, handleSubmit, handleReset } = useFormBee({ callback, rules }); 
Parameter

useformbee takes an object as its parameter. The object parameter must have two attribute callback and rules.

  • The callback is the function that will be called when the form is submitted and passes all validation.

  • The rules is an object of validatorjs rules

Returns

useformbee returns an object of 5 attributes.

  • values (object)
  • errors (object)
  • handleChange (function)
  • handleSubmit (function)
  • handleReset (function)

Form Example

const Form = () => {
  // prepare your inputs rules
  // read more about validatorjs rule -> https://www.npmjs.com/package/validatorjs
  const rules = {
    firstName: 'alpha|required',
    age: 'numeric',
  };

  // create you callback function
  const saveFormData = (values) => {
    // ...your logic
  };

  const {
    values, handleChange, handleSubmit, errors, handleReset,
  } = useFormBee({ callback: saveFormData, rules });

  // destructure field values

  // the values is created from Object.keys(rules)
  const { firstName, age } = values;

  return (
    <form onSubmit={handleSubmit} onReset={handleReset}>
      <input
        type='text'
        value ={firstName}
        onChange={handleChange}
        name='firstName'
        required
      />

      {/* display username error  if there is an error */}
      {errors.firstName && <p>{errors.firstName }</p>}
      <br />
    
      <input
        type='text'
        value ={age}
        onChange={handleChange}
        name='age'
      />

      {/* display age error  if there is an error */}
      {errors.age && <p>{errors.age}</p>}
      <br />

      <button type='submit'>Submit</button>
      <button type='reset'>Reset</button>
    </form>
  );
};

Contributors

Thanks goes to these people (emoji key)

LICENSE