Skip to content

Commit 85979f3

Browse files
committed
Prevents onChange trigger on first render
1 parent 82fe578 commit 85979f3

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/components/FormContext.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ export const FormContext: FC<FormContextProps> = props => {
3535
submitFocusError: submitFocusError,
3636
})
3737

38-
if (typeof onChange === 'function') {
39-
onChange(getObjectFromForm(props.schema, methods.watch()))
38+
const isFirstRender = React.useRef(true)
39+
40+
const watchedInputs = methods.watch()
41+
42+
if (isFirstRender.current === true) {
43+
isFirstRender.current = false
44+
} else if (typeof onChange === 'function') {
45+
onChange(getObjectFromForm(props.schema, watchedInputs))
4046
}
4147

4248
const idMap = useMemo(() => getIdSchemaPairs(props.schema), [props.schema])

0 commit comments

Comments
 (0)