1.0.19 • Published 4 years ago
hookable-unform v1.0.19
hookable-unform
A set of React Hooks to use with Unform
Getting Started
yarn add hookable-unform yup @unform/core
ornpm i hookable-unform yup @unform/core
yup
and@unform/core
are required as peer dependencies
Hooks
All hooks have two variations, the one that receives the form reference as a parameter and the one that returns a reference to be used later, this second one always suffixes the word Ref
import { useRef } from 'react';
import { useFormValidator, useFormValidatorRef } from 'hookable-unform';
import schema from './schema'; // Yup Schema
const formRef = useRef(null);
const validateForm = useFormValidator(formRef, schema);
const [formRef, validateForm] = useFormValidatorRef(schema);
useFormValidatorRef
useFormValidator execute validation on your form based in a Yup Schema.
import { useFormValidatorRef } from 'hookable-unform';
import schema from './schema'; // Yup Schema
const [formRef, validateForm] = useFormValidatorRef(schema);
const handleSubmit = useCallback(async () => {
const validationResult = await validateForm();
if (validationResult.success) {
console.log(validationResult.data);
} else {
console.log(validationResult.errors);
}
}, [validateForm]);
<Form onSubmit={handleSubmit} ref={formRef}>
{/* inputs */}
</Form>;
Options in returned function of hook
applyErrors: boolean
: set if automatically apply founded errors, default is true
useFormHandlersRef
userFormHandlers execute the ref null verification, and you can call function without Object is possibly 'null'
, exclude use of ?.
operator.
import { useFormHandlersRef } from 'hookable-unform';
const [formRef, { getData, setData, submitForm, ...rest }] = useFormHandlersRef();
const handleSubmit = useCallback(async () => {
console.log(getData());
}, [validateForm]);
<Form onSubmit={handleSubmit} ref={formRef}>
{/* inputs */}
</Form>;
1.0.19
4 years ago
1.0.18
4 years ago
1.0.17
4 years ago
1.0.16
4 years ago
1.0.15
4 years ago
1.0.14
4 years ago
1.0.13
4 years ago
1.0.12
4 years ago
1.0.11
4 years ago
1.0.10
4 years ago
1.0.9
4 years ago
1.0.8
4 years ago
1.0.7
4 years ago
1.0.6
4 years ago
1.0.5
4 years ago
1.0.4
4 years ago
1.0.3
4 years ago
1.0.1
4 years ago
1.0.0
4 years ago