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

vanilla-hooks

v3.0.3

Published

a simple collection of fast hooks, including work with forms and api

Downloads

10

Readme

Introduction

A kit of hooks that simplify work with forms and fetch. Typescript and yup are included out of box.

Installation

NPM

npm i vanilla-hooks

YARN

yarn add vanilla-hooks

Hooks

How to use useVanillaForm

A react hook that uses dom for the best optimisation forms, it allows to work with large forms that consists of hundreds of inputs.

Reason of creating

The hook was designed not looking at such popular instruments as formik and etcetera, because formik in comparing with current hook is very slow, because it uses hook 'useState' under the hood that can't cope with large forms, it just lags. The hook has almost the same of functional formik, but it is faster in some times thanks dom.

Restrictions

These restrictions are temporary

  • Type mixed isn't available
  • 2D array aren'y available

API

Parameters

schema: ObjectSchema<Shape<any, any>>

Yup schema is required to bind the form model(fields that have attribute "name") and validation.

initialValues?: T

The field isn't required, you can build form without initialisation values. The values will bind to name attributes of dom. For example company[0].sex = <input name='company[0].sex' />

valuesAnyway?: boolean

The field is false by default, if you need to get values in submit even durin an error, then you can set the field as true.

allFieldsExisted?: boolean

The field is true by default, you should use to set the field as false, when your form is dynamic, otherwise it will give an error.

onSubmit: (isValid: boolean, values: T, errors?: any) => void

Function that handles after using handleSubmit.

Returned Variables

errors: { [key: string]: string }

You can get every error message with help of key. For example errors['company[0].sex'].

handleSubmit: () => void

Function that handles parameter onSubmit. Use with a form.

setValues: () => void;

Set values in dom.

getValues: () => T

Return values in real time.

Examples

import React from 'react';
import { useVanillaForm } from 'vanilla-hooks';
import { object, array, string, number, bool } from 'yup'

const yupSchema = object().shape({
    name: string(),
    age: number.max(20, "you need to be younger than 20",
    company: array().of(object({
        address: string(),
        name: string().required(),
        sex: bool()
    }))
});

function App() {
    const { errors, handleSubmit } = useVanillaForm({ // you can get errors inside submit or as the variable
        schema: yupSchema,
        onSubmit: (isValid, values, errors) => {
            if (isValid) {
                console.log("values", values);
            } else {
                console.log("errors", errors);
            }
        },
        allFieldsExisted: false,
        initialValues: { 
            name: "Giants",
            age: 2,
            company: [
                {
                    name: "Apple",
                    address: "somewhere",
                    sex: true
                },
                {
                    address: "anywhere"
                },
                {
                    sex: false
                }
            ]
        }
    });

    function submit(e) {
        e.preventDefault();
        handleSubmit();
    }

    return (
        <form onSubmit={submit}>
            <div>
                <label>Name: </label>
                <input name="name" />
                <span>{errors["name"]}</span>
            </div>
            <div>
                <label>Age: </label>
                <input name="age" />
                <span>{errors["age"]}</span>
            </div>
            <div>
                <label>Companies: </label>
                <div>
                    <input name="company[0].name" />
                    <span>{errors["company[0].name"]}</span>
                </div>
                <div>
                    <input name="company[0].sex" type="checkbox" />
                    <span>{errors["company[0].sex"]}</span>
                </div>
            </div>
            <button type="submit">Submit</button>
        </form>
    );
}

export default App;

How to use useVanillaFetch

The hook is not ready to production