1
1
import React , { useEffect , useRef , useState } from "react" ;
2
2
import { ChildFormState , DirtyMap , ErrorMap , FormState } from "./form" ;
3
- import { useArrayForm , useListener , useAnyListener , useChildForm } from "./hooks" ;
3
+ import { useArrayForm , useListener , useAnyListener , useChildForm , useTruthyListener } from "./hooks" ;
4
4
5
5
/**
6
6
* Wrapper around useArrayForm (which is a wrapper around useChildForm).
7
7
* Exports useful functions to manipulate arrays.
8
- * This hook does cause a rerender, but only if the whole array changes .
8
+ * This hook does cause a rerender, but only if the whole array becomes null/undefined .
9
9
* @param parent The parent form.
10
10
* @param name The parent's field to create a child form for.
11
11
*/
@@ -24,20 +24,9 @@ export function ArrayForm<Parent, ParentState, ParentError, Key extends keyof Pa
24
24
} ) => React . ReactNode ;
25
25
} ) {
26
26
const childForm = useArrayForm ( props . form , props . name ) ;
27
- const oldThruthly = useRef ( ! ! props . form . values [ props . name ] ) ;
28
- const [ , setRender ] = useState ( 0 ) ;
29
27
30
- // Rerender when array became null/not null (thruthly/falsely)
31
- useEffect ( ( ) => {
32
- let id = props . form . listen ( props . name , ( ) => {
33
- let thruthly = ! ! props . form . values [ props . name ] ;
34
- if ( thruthly !== oldThruthly . current ) {
35
- setRender ( ( i ) => i + 1 ) ;
36
- oldThruthly . current = thruthly ;
37
- }
38
- } ) ;
39
- return ( ) => props . form . ignore ( props . name , id ) ;
40
- } , [ ] ) ;
28
+ // Causes a rerender when the array becomes null/not null
29
+ useTruthyListener ( props . form , props . name ) ;
41
30
42
31
// Do not render anything if the parent field is falsly
43
32
if ( ! props . form . values [ props . name ] ) return null ;
@@ -85,7 +74,7 @@ export function AnyListener<T, State, Error>(props: {
85
74
/**
86
75
* Wrapper around useChildForm
87
76
* Creates a child form for another root or child form. You must use this for object and array (see useArrayForm) fields.
88
- * This hook doesn't cause a rerender.
77
+ * This hook does cause a rerender, but only if the object field becomes null/undefined .
89
78
* @param parentForm The parent form.
90
79
* @param name The parent's field to create a child form for.
91
80
*/
@@ -95,20 +84,8 @@ export function ChildForm<Parent, ParentState, ParentError, Key extends keyof Pa
95
84
render ?: ( props : ChildFormState < Parent , ParentState , ParentError , Key > ) => React . ReactNode ;
96
85
} ) {
97
86
const childForm = useChildForm ( props . form , props . name ) ;
98
- const oldThruthly = useRef ( ! ! props . form . values [ props . name ] ) ;
99
- const [ , setRender ] = useState ( 0 ) ;
100
-
101
- // Only rerender when object became null/not null (thruthly/falsely)
102
- useEffect ( ( ) => {
103
- let id = props . form . listen ( props . name , ( ) => {
104
- let thruthly = ! ! props . form . values [ props . name ] ;
105
- if ( thruthly !== oldThruthly . current ) {
106
- setRender ( ( i ) => i + 1 ) ;
107
- oldThruthly . current = thruthly ;
108
- }
109
- } ) ;
110
- return ( ) => props . form . ignore ( props . name , id ) ;
111
- } , [ ] ) ;
87
+ // Causes a rerender when the object value becomes null/undefined
88
+ useTruthyListener ( props . form , props . name ) ;
112
89
113
90
// Do not render anything if the parent field is falsly
114
91
if ( ! props . form . values [ props . name ] ) return null ;
0 commit comments