File tree 1 file changed +12
-4
lines changed 1 file changed +12
-4
lines changed Original file line number Diff line number Diff line change @@ -7,9 +7,17 @@ export const all = formSubscriptionItems.reduce((result, key) => {
7
7
} , { } )
8
8
9
9
const useForm = ( { subscription, ...config } ) => {
10
- const form = useRef ( createForm ( config ) )
10
+ const form = useRef ( )
11
+ // https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily
12
+ const getForm = ( ) => {
13
+ if ( ! form . current ) {
14
+ form . current = createForm ( config )
15
+ }
16
+
17
+ return form . current
18
+ }
11
19
const [ state , setState ] = useState ( { } )
12
- useEffect ( ( ) => form . current . subscribe ( setState , subscription || all ) , [
20
+ useEffect ( ( ) => getForm ( ) . subscribe ( setState , subscription || all ) , [
13
21
subscription
14
22
] )
15
23
const handleSubmit = useCallback ( event => {
@@ -21,10 +29,10 @@ const useForm = ({ subscription, ...config }) => {
21
29
event . stopPropagation ( )
22
30
}
23
31
}
24
- return form . current . submit ( )
32
+ return getForm ( ) . submit ( )
25
33
} , [ ] )
26
34
27
- return { ...state , form : form . current , handleSubmit }
35
+ return { ...state , form : getForm ( ) , handleSubmit }
28
36
}
29
37
30
38
export default useForm
You can’t perform that action at this time.
0 commit comments