useForm Hook

Manage form state with validation

ReactFormsValidation

useForm

Form state management with validation

Copy source code and paste it in hooks/devsloka-hooks folder

hooks/devsloka-hooks/use-form.tsx

1"use client";
2import { useState } from "react";
3
4export const useForm = <T extends Record<string, unknown>>(
5 initialValues: T
6): [T, (e: React.ChangeEvent<HTMLInputElement>) => void] => {
7 const [values, setValues] = useState<T>(initialValues);
8
9 const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
10 setValues({
11 ...values,
12 [e.target.name]: e.target.value,
13 });
14 };
15
16 return [values, handleChange];
17};