Getting Started
Get started with useMuiForm - a lightweight wrapper around react-hook-form for Material-UI
useMuiForm is a lightweight wrapper around react-hook-form that provides seamless integration with Material-UI components. It handles the boilerplate of connecting MUI inputs to react-hook-form while maintaining full access to all react-hook-form features.
Why useMuiForm?
react-hook-form returns props like onChange, onBlur, ref, etc. But MUI components expect:
error(boolean) instead of checking errors manuallyhelperText(string) for displaying error messagesinputRefinstead ofrefcheckedfor checkboxes instead ofvalue
useMuiForm's enhanced register function automatically provides these MUI-compatible props.
Installation
Choose your package manager:
npm i usemuiform react-hook-formpnpm add usemuiform react-hook-formyarn add usemuiform react-hook-formbun add usemuiform react-hook-formQuick Start
import { useMuiForm } from 'usemuiform'
import { TextField, Button, Stack } from '@mui/material'
type FormState = {
email: string
name: string
}
function MyForm() {
const { register, handleSubmit } = useMuiForm<FormState>({
defaultValues: { email: '', name: '' }
})
const onSubmit = (data: FormState) => {
console.log('Form data:', data)
}
return (
<Stack spacing={2}>
<TextField
label="Email"
type="email"
{...register('email', {
required: 'Email is required',
pattern: {
value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
message: 'Invalid email address'
}
})}
/>
<TextField
label="Name"
{...register('name', { required: 'Name is required' })}
/>
<Button variant="contained" onClick={handleSubmit(onSubmit)}>
Submit
</Button>
</Stack>
)
}What You Get
All react-hook-form features work as normal:
- All validation options (required, pattern, min, max, validate, etc.)
- All modes (onChange, onBlur, onSubmit, etc.)
- Schema validation (Zod, Yup, etc.)
- All hook methods (watch, setValue, trigger, reset, etc.)
Plus MUI-compatible props from register:
error- boolean indicating validation errorhelperText- error message stringinputRef- proper ref for MUI inputschecked- for checkbox/switch components
Next Steps
- Usage with MUI Components - Examples for TextField, Select, Checkbox, DatePicker
- react-hook-form Documentation - For validation, modes, and advanced features
- API Reference - Complete API documentation