Skip to content

Commit

Permalink
added check for undefined or null (#224)
Browse files Browse the repository at this point in the history
* added check for undefined or null

* Add comment

Co-authored-by: Strahinja Ajvaz <strahinja.ajvaz@latitudefinancial.com>
Co-authored-by: Misha Moroshko <michael.moroshko@gmail.com>
  • Loading branch information
3 people authored May 5, 2021
1 parent 8d33a8d commit 36caa1d
Showing 1 changed file with 13 additions and 1 deletion.
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;

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

1 comment on commit 36caa1d

@vercel
Copy link

@vercel vercel bot commented on 36caa1d May 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.