Skip to content

Commit

Permalink
🥌 improve useFormState with mounted ref to skip update when hook is u…
Browse files Browse the repository at this point in the history
…nmounted (react-hook-form#7489)
  • Loading branch information
bluebill1049 authored Jan 11, 2022
1 parent 9b1dcf9 commit cf2c633
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/useFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ function useFormState<TFieldValues extends FieldValues = FieldValues>(
errors: false,
});
const _name = React.useRef(name);
const _mounted = React.useRef(true);

_name.current = name;

const callback = React.useCallback(
(value) =>
_mounted.current &&
shouldSubscribeByName(
_name.current as InternalFieldName,
value.name,
Expand All @@ -51,6 +53,13 @@ function useFormState<TFieldValues extends FieldValues = FieldValues>(
subject: control._subjects.state,
});

React.useEffect(
() => () => {
_mounted.current = false;
},
[],
);

return getProxyFormState(
formState,
control._proxyFormState,
Expand Down

0 comments on commit cf2c633

Please sign in to comment.