Files
openmonetis/hooks/use-form-state.ts
Felipe Coutinho 757626c468 refactor: optimize codebase for React 19 compiler (v1.2.6)
React 19 compiler auto-optimizes memoization, making manual hooks unnecessary.

Changes:
- Remove ~60 useCallback/useMemo across 16 files
- Remove React.memo from nav-button and return-button
- Simplify hydration with useSyncExternalStore (privacy-provider)
- Add CHANGELOG.md for version tracking

No functional changes - internal optimization only.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-04 13:14:10 +00:00

51 lines
1.0 KiB
TypeScript

import { useState } from "react";
/**
* Hook for managing form state with type-safe field updates
*
* @param initialValues - Initial form values
* @returns Object with formState, updateField, resetForm, setFormState
*
* @example
* ```tsx
* const { formState, updateField, resetForm } = useFormState({
* name: '',
* email: ''
* });
*
* updateField('name', 'John');
* ```
*/
export function useFormState<T extends Record<string, any>>(initialValues: T) {
const [formState, setFormState] = useState<T>(initialValues);
/**
* Updates a single field in the form state
*/
const updateField = <K extends keyof T>(field: K, value: T[K]) => {
setFormState((prev) => ({ ...prev, [field]: value }));
};
/**
* Resets form to initial values
*/
const resetForm = () => {
setFormState(initialValues);
};
/**
* Updates multiple fields at once
*/
const updateFields = (updates: Partial<T>) => {
setFormState((prev) => ({ ...prev, ...updates }));
};
return {
formState,
updateField,
updateFields,
resetForm,
setFormState,
};
}