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

use-handle-change

v2.2.9

Published

A custom [React Hook](https://reactjs.org/docs/hooks-overview.html) that you probably don't need for managing form state as an object.

Downloads

25

Readme

Use Handle Change

A custom React Hook that you probably don't need for managing form state as an object.

npm

Motivation

Built both to satisfy an itch and to abstract away repetitive functions throughout react apps.

A React Hook You Don't Need

Features

  • You still keep control over your elements.
  • Handles most if not all input types.
  • Inject middleware functions for custom validation.
  • Target deeply nested object values dynamically.
  • Receive new state as a callback.
  • Pass in a Constructor function to parse your initial state into a desired shape.
  • Ability to reset state to initial state.

Requirement

To use use-handle-change, you must use [email protected] or greater which includes hooks.

Installation

$ npm i use-handle-change

Event Object

When running your handleChange function against any input during the onChange event, you may pass in the two following arguments to the function.

If you do not pass any arguments, and place your handleChange function on the onChange attribute, it will behave normally with the passing of the event object to the function.

/**
 * @param {Object} initialState - The object shape you want to start your form data as with key value pairs.
 * @param {function} Model - Optional, the constructor function used to format your initial state and state upon reset.
 */
useHandleChange(initialState, Model)
/**
 * @param {Object} event - The event object from the DOM input
 * @param {Object} config - A list of configurations you can pass to the state manager to invoke actions
 */
handleChange(event, config)
// Config Object
{
  callback: _myCallbackFunction, // A function that will receive the updated state for use after update
  keys: [], // Array of keys for settings the value of a deeply nested key within your state object
  reset: // Resets state to initial state passed into hook.
}

Example

Full Component

import React from "react";
import { useHandleChange } from "use-handle-change";

const DEFAULT_STATE = {
  name: "",
  email: "",
};

const ReactForm = () => {
  const [state, handleChange] = useHandleChange(DEFAULT_STATE);

  function _handleSubmit(e) {
    e.preventDefault();
    //... form submission logic
  }

  return (
    <form onSubmit={_handleSubmit}>
      <div>
        <div>
          <label htmlFor="inputName">Name</label>
        </div>
        <input
          id="inputName"
          type="text"
          name="name"
          value={state.name}
          onChange={handleChange}
        />
      </div>
      <div>
        <div>
          <label htmlFor="inputEmail">Email</label>
        </div>
        <input
          id="inputEmail"
          type="email"
          name="test"
          value={state.email}
          onChange={handleChange}
        />
      </div>
      <button type="submit">Submit</button>
    </form>
  );
};

Checkbox

const DEFAULT_STATE = {
  checkbox: false,
};

const [state, handleChange] = useHandleChange(DEFAULT_STATE);

return (
  <input
    id="inputCheckbox"
    type="checkbox"
    name="checkbox"
    checked={state.checkbox}
    onChange={handleChange}
  />
);

Nested Object

const DEFAULT_STATE = {
  parent: {
    child: "",
  },
};

const [state, handleChange] = useHandleChange(DEFAULT_STATE);

return (
  <input
    type="text"
    name="child"
    value={state.parent.child}
    onChange={(e) => handleChange(e, { keys: ["parent", "child"] })}
  />
);

Callback

const DEFAULT_STATE = {
  name: "",
};

const [state, handleChange] = useHandleChange(DEFAULT_STATE);

/**
 * @param {Object} obj - The object from the hooked state for you use as you please.
 */
function _callbackFunc(obj) {
  //... run other code here...
}

return (
  <input
    type="text"
    name="name"
    value={state.name}
    onChange={(e) => handleChange(e, { callback: _callbackFunc })}
  />
);

License

MIT Licensed

Contributors

Michael McShinsky