@@ -12,6 +12,7 @@ import {
1212 UseFormProps ,
1313} from 'react-hook-form' ;
1414import merge from 'lodash/merge.js' ;
15+ import isEmpty from 'lodash/isEmpty.js' ;
1516import { RaRecord } from '../types' ;
1617import { SaveHandler , useRecordContext , useSaveContext } from '../controller' ;
1718import getFormInitialValues from './getFormInitialValues' ;
@@ -83,25 +84,29 @@ export const useAugmentedForm = <RecordType = any>(
8384 } ) ;
8485
8586 const formRef = useRef ( form ) ;
86- const { reset } = form ;
87+ const { reset, formState } = form ;
88+ const { isReady } = formState ;
8789
8890 useEffect ( ( ) => {
91+ if ( ! isReady ) return ;
92+ if ( isEmpty ( defaultValuesIncludingRecord ) ) return ;
8993 reset ( defaultValuesIncludingRecord ) ;
90- } , [ defaultValuesIncludingRecord , reset ] ) ;
94+ } , [ defaultValuesIncludingRecord , reset , isReady ] ) ;
9195
9296 // notify on invalid form
9397 useNotifyIsFormInvalid ( form . control , ! disableInvalidFormNotification ) ;
9498
9599 const recordFromLocation = useRecordFromLocation ( ) ;
96100 const recordFromLocationApplied = useRef ( false ) ;
97101 useEffect ( ( ) => {
102+ if ( ! isReady ) return ;
98103 if ( recordFromLocation && ! recordFromLocationApplied . current ) {
99104 reset ( merge ( { } , defaultValuesIncludingRecord , recordFromLocation ) , {
100105 keepDefaultValues : true ,
101106 } ) ;
102107 recordFromLocationApplied . current = true ;
103108 }
104- } , [ defaultValuesIncludingRecord , recordFromLocation , reset ] ) ;
109+ } , [ defaultValuesIncludingRecord , recordFromLocation , reset , isReady ] ) ;
105110
106111 // submit callbacks
107112 const handleSubmit = useCallback (
0 commit comments