Skip to content

ravisojitra/simple-react-hook-form

Repository files navigation

simple-react-hook-form

A simple form library for react.js and react native using hooks

NPM JavaScript Style Guide

Edit nostalgic-cookies-xmq8g

Install

npm install --save simple-react-hook-form

Usage

import React, { Component } from 'react'

import useForm from 'simple-react-hook-form'
import * as yup from 'yup'

let initialValues = { name: 'React' }
const validationSchema = yup.object().shape({
  name: yup
    .string()
    .min(5, 'Name must be minimum of length 5')
    .required('Name is required')
})
const App = () => {
  const {
    handleChange,
    resetForm,
    clearForm,
    values,
    handleSubmit,
    errors
  } = useForm(initialValues, {
    validate: true,
    validateSchema: validationSchema,
    validationOnChange: true
  })

  const onSubmit = () => {
    console.log('form is valid, Do what you gotta do!')
  }
  return (
    <div className='App'>
      <form>
        <div>
          <label htmlFor={'Name'}>{'Name'}</label>
          <input
            onChange={(e) => handleChange('name', e.currentTarget.value)}
            name={'name'}
            placeholder={'Please enter name'}
            type={'text'}
            value={values.name}
          />
          {errors.name && <p className='errorText'>{errors.name.join('\n')}</p>}
        </div>

        <button type='button' onClick={() => handleSubmit(onSubmit)}>
          Submit
        </button>
      </form>
    </div>
  )
}

License

MIT © ravisojitra

About

A simple form library for react.js and react native apps

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published