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>
51 lines
1.0 KiB
TypeScript
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,
|
|
};
|
|
}
|