From 5c125b9a48fe743b865401a64ac29bfe1f631b94 Mon Sep 17 00:00:00 2001 From: zhujinxuan Date: Sat, 18 Dec 2021 14:33:03 -0500 Subject: [PATCH] Fix rendering while updating in FormSpy and useFormState --- src/useFormState.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/useFormState.js b/src/useFormState.js index 3602d62..83be72f 100644 --- a/src/useFormState.js +++ b/src/useFormState.js @@ -22,13 +22,19 @@ function useFormState({ form.subscribe((state) => { initialState = state; }, subscription)(); - if (onChange) { - onChange(initialState); - } return initialState; }, ); + React.useEffect( + () => { + if (onChange) { + onChange(state); + } + }, + // eslint-disable-next-line react-hooks/exhaustive-deps + [], + ); React.useEffect( () => form.subscribe((newState) => {