33 MutableRefObject ,
44 SetStateAction ,
55 useEffect ,
6- useLayoutEffect ,
76 useMemo ,
87 useReducer ,
98 useRef ,
@@ -21,9 +20,6 @@ import {
2120 Validity ,
2221} from "./types" ;
2322
24- // For server-side rendering / react-native
25- const useIsoLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect ;
26-
2723export const useForm = < Values extends Required < Values > , ErrorMessage = string > (
2824 config : FormConfig < Values , ErrorMessage > ,
2925) : Form < Values , ErrorMessage > => {
@@ -32,12 +28,10 @@ export const useForm = <Values extends Required<Values>, ErrorMessage = string>(
3228
3329 const [ , forceUpdate ] = useReducer ( ( ) => [ ] , [ ] ) ;
3430 const mounted = useRef ( false ) ;
35- const arg = useRef ( config ) ;
3631 const formStatus = useRef < FormStatus > ( "untouched" ) ;
3732
38- useIsoLayoutEffect ( ( ) => {
39- arg . current = config ;
40- } ) ;
33+ const arg = useRef ( config ) ;
34+ arg . current = config ;
4135
4236 useEffect ( ( ) => {
4337 mounted . current = true ;
@@ -370,25 +364,27 @@ export const useForm = <Values extends Required<Values>, ErrorMessage = string>(
370364 } ;
371365 } , [ ] ) ;
372366
373- // Lazy initialization
374- if ( ! fields . current ) {
375- fields . current = { } as ( typeof fields ) [ "current" ] ;
376-
377- for ( const name in arg . current ) {
378- if ( Object . prototype . hasOwnProperty . call ( arg . current , name ) ) {
379- fields . current [ name ] = {
380- callbacks : new Set ( ) ,
381- ref : { current : null } ,
382- mounted : false ,
383- state : {
384- value : arg . current [ name ] . initialValue ,
385- talkative : false ,
386- validity : { tag : "unknown" } ,
387- } ,
388- } ;
389- }
367+ const tmp = { } as ( typeof fields ) [ "current" ] ;
368+
369+ for ( const name in arg . current ) {
370+ if ( Object . prototype . hasOwnProperty . call ( arg . current , name ) ) {
371+ tmp [ name ] = fields . current ?. [ name ] ?? {
372+ callbacks : new Set ( ) ,
373+ ref : { current : null } ,
374+ mounted : false ,
375+ state : {
376+ value : arg . current [ name ] . initialValue ,
377+ talkative : false ,
378+ validity : { tag : "unknown" } ,
379+ } ,
380+ } ;
390381 }
382+ }
391383
384+ fields . current = tmp ;
385+
386+ // Lazy initialization
387+ if ( ! field . current ) {
392388 const Field : Contract [ "Field" ] = ( { name, children } ) => {
393389 const { subscribe, getSnapshot } = useMemo (
394390 ( ) => ( {
0 commit comments