Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added check for undefined or null #224

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,19 @@ function Form(_props) {
};
const getFieldErrors = useCallback((values, name) => {
const value = getPath(values, name);
const field = fields.current[name].current;
/*
Note:
`getFieldErrors` is called by `useEffect` below when `namesToValidate` change,
and we set `namesToValidate` inside `setTimeout` in `onBlur`. This means that
`getFieldErrors` will be called with a little delay.
This opens the door for `unregisterField` being called BEFORE `getFieldErrors` is called.
Think about an input field being focused and then the user clicks on a Next button which
unmounts the current form and mounts the next form page.
In this case, `getFieldErrors` will be called with a `name` that doesn't exist in `fields.current`
anymore since `unregisterField` deleted it.
That's why we need to take case of this scenario :)
*/
const field = fields.current[name]?.current;
moroshko marked this conversation as resolved.
Show resolved Hide resolved

if (
!field || // See: https://stackoverflow.com/q/65659161/247243
Expand Down