
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
react-simple-hooks-forms
Advanced tools
Performatic forms for React/React Native using hooks api. Yes we're running out of names for React Forms with hooks.
Performatic forms for React/React Native using hooks api. Yes we're running out of names for React Forms with hooks.
Typescript typings available.
yarn add react-simple-hooks-forms
or
npm i react-simple-hooks-forms
Build your inputs: (works with React and React Native)
import React from 'react'
import { useFormInput } from 'react-simple-hooks-forms'
export const TextFormField = (props) => {
const { value, setValue } = useFormInput(props)
const onChange = (e) => setValue(e.target.value)
return <input
name={props.name}
value={value}
onChange={onChange}
/>
}
Wrap your fields with the Form
component:
import React from 'react'
import { useForm } from 'react-simple-hooks-forms'
import { TextFormField } from './TextFormField'
const validator = (values) => {
const errors = {}
if (!values.name) {
errors.name = 'Name is required'
}
return errors
}
const initialValues = { name: 'John' }
const App = () => {
const { Form, submit, reset } = useForm({ initialValues, validator })
return <Form>
<TextFormField name={'name'} />
<TextFormField name={'email'} />
<button onClick={submit}>
Submit
</button>
<button onClick={reset}>
Reset
</button>
</Form>
}
In many cases you may need to do something based on the value of a field, like changing the value of another field for
instance. For this you may use the hook useFormFieldValue
, and it works in any component that is wrapped with the Form
context.
import { useForm, useFormFieldValue } from 'react-simple-hooks-forms'
const getPasswordStrength = (password) => {
if (password.length >= 20) {
return 'very strong'
} else if (password.length >= 10) {
return 'strong'
} else if (password.length >= 6) {
return 'adequate'
}
return 'week'
}
const PasswordStrength = () => {
const { value: passwordValue } = useFormFieldValue('password')
return <div>{`This password is ${getPasswordStrength(passwordValue)}`}</div>
}
export default () => {
const { Form } = useForm()
return <Form>
<TextFormField name={'password'} />
<PasswordStrength />
</Form>
}
Although not recommended in most cases because of performance issues, you may use it outside the Form context by
providing a name to both your useForm
and your useFormFieldValue
hooks:
const formName = 'LoginForm'
export default () => {
const { Form } = useForm({ formName })
const { value: passwordValue } = useFormFieldValue('password', { formName })
return <Form>
<TextFormField name={'password'} />
<div>{`This password is ${getPasswordStrength(passwordValue)}`}</div>
</Form>
}
You may use this hook to set the values of fields as well. In the next example we fill a username field based on the name
field. You can pass a configuration like { setterOnly: true }
if you do not wish to listen to the value and simply have
a setter for it.
const formName = 'SignUpForm'
export default () => {
const { Form } = useForm({ formName })
const { value: fullNameValue } = useFormFieldValue('fullName', { formName })
const { set: setUsername } = useFormFieldValue('username', { formName, setterOnly: true })
useEffect(() => {
setUsername(nameValue.trim().toLowerCase())
}, [nameValue])
return <Form>
<TextFormField name={'fullName'} />
<TextFormField name={'username'} />
</Form>
}
This lib provides form and field-level validation. To use form-level validation simply pass a validator
function to
the useForm
hook. This function receives a FormValues object and should return an object containing
the error messages. You might want to use a third party library to handle this, like ValidateJs.
const validator = (values) => {
const errors = {}
if (!values.name) {
errors.name = 'Name is required'
} else if (values.name.length < 2) {
errors.name = 'Name is too short'
}
return errors
}
export default () => {
const { Form, submit } = useForm({ validator })
return <Form>
<TextFormField name={'name'} />
<button onClick={submit}>
Submit
</button>
</Form>
}
You can use field-level validation by passing a validator
prop to your wrapped input component. This will by default
validate the field on every input change.
const passwordValidator = (password) => {
if (password.length < 6) {
return 'Password is too short'
}
}
export default () => {
const { Form } = useForm()
return <Form>
<TextFormField
name={'password'}
validator={passwordValidator}
/>
</Form>
}
You can choose to validate on the field's onBlur:
const TextFormField = (props) => {
const { value, setValue } = useFormInput(props)
const onChange = (e) => setValue(e.target.value)
return <input
name={props.name}
value={value}
onChange={onChange}
/>
}
export default () => {
const { Form } = useForm()
return <Form>
<TextFormField
name={'password'}
validate={passwordValidator}
validationOptions={{
trigger: ValidationOptions.BLUR,
}}
/>
</Form>
}
Async Validation
Documentation on Api Reference
FAQs
Performatic forms for React/React Native using hooks api. Yes we're running out of names for React Forms with hooks.
We found that react-simple-hooks-forms demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.