@@ -4,6 +4,7 @@ import { useFormContext, FieldValues } from 'react-hook-form';
44import get from 'lodash/get' ;
55import { useFormValues } from './useFormValues' ;
66import { useWrappedSource } from '../core' ;
7+ import { useEvent } from '../util' ;
78
89/**
910 * Get the current (edited) value of the record from the form and pass it
@@ -67,22 +68,26 @@ export const FormDataConsumerView = <
6768 props : Props < TFieldValues >
6869) => {
6970 const { children, formData, source } = props ;
70- let result ;
71+ const [ result , setResult ] = React . useState < ReactNode > ( null ) ;
7172
7273 const finalSource = useWrappedSource ( source || '' ) ;
74+ const render = useEvent ( children ) ;
7375
74- // Passes an empty string here as we don't have the children sources and we just want to know if we are in an iterator
75- const matches = ArraySourceRegex . exec ( finalSource ) ;
76+ // Getting the result of the children function in a useEffect allows us to keep a stable reference to is
77+ // with useEvent
78+ React . useEffect ( ( ) => {
79+ // Passes an empty string here as we don't have the children sources and we just want to know if we are in an iterator
80+ const matches = ArraySourceRegex . exec ( finalSource ) ;
81+ // If we have an index, we are in an iterator like component (such as the SimpleFormIterator)
82+ if ( matches ) {
83+ const scopedFormData = get ( formData , matches [ 0 ] ) ;
84+ setResult ( render ( { formData, scopedFormData } ) ) ;
85+ } else {
86+ setResult ( render ( { formData } ) ) ;
87+ }
88+ } , [ finalSource , formData , render ] ) ;
7689
77- // If we have an index, we are in an iterator like component (such as the SimpleFormIterator)
78- if ( matches ) {
79- const scopedFormData = get ( formData , matches [ 0 ] ) ;
80- result = children ( { formData, scopedFormData } ) ;
81- } else {
82- result = children ( { formData } ) ;
83- }
84-
85- return result === undefined ? null : result ;
90+ return result ;
8691} ;
8792
8893const ArraySourceRegex = new RegExp ( / .+ \. \d + $ / ) ;
0 commit comments