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

Fix Form useEffect #20091

Merged
merged 8 commits into from
Jun 16, 2023
Merged
Changes from 2 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
22 changes: 16 additions & 6 deletions src/components/Form.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState, useEffect, useCallback, useRef} from 'react';
import React, {useState, useEffect, useCallback, useMemo, useRef} from 'react';
import lodashGet from 'lodash/get';
import {Keyboard, ScrollView, StyleSheet} from 'react-native';
import PropTypes from 'prop-types';
Expand Down Expand Up @@ -94,6 +94,7 @@ const Form = (props) => {
const formContentRef = useRef(null);
const inputRefs = useRef({});
const touchedInputs = useRef({});
const isFirstRender = useRef(true);

const {validate, translate, onSubmit, children} = props;

Expand Down Expand Up @@ -145,10 +146,19 @@ const Form = (props) => {
);

useEffect(() => {
// We want to skip Form validation on initial render.
// This also avoids a bug where we immediately clear server errors when the loading indicator unmounts and Form remounts with server errors.
if (isFirstRender.current) {
isFirstRender.current = false;
return;
}

onValidate(inputValues);
}, [onValidate, inputValues]);

const getErrorMessage = useCallback(() => {
// eslint-disable-next-line react-hooks/exhaustive-deps -- we just want to revalidate the form on update if the preferred locale changed on another device so that errors get translated
}, [props.preferredLocale]);

const errorMessage = useMemo(() => {
const latestErrorMessage = ErrorUtils.getLatestErrorMessage(props.formState);
return props.formState.error || (typeof latestErrorMessage === 'string' ? latestErrorMessage : '');
Copy link
Member

Choose a reason for hiding this comment

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

NAB: I think there's a typo here? There is no such field called formState.error

It's formState.errors, right? (notice the "s")

https://github.com/Expensify/App/pull/20091/files#diff-a54e851a201173befe09914aee40247c3825f28fa4ff3f01eeaa500be75c5dc8R46

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, you're right. Fixed!

}, [props.formState]);
Expand Down Expand Up @@ -324,9 +334,9 @@ const Form = (props) => {
{props.isSubmitButtonVisible && (
<FormAlertWithSubmitButton
buttonText={props.submitButtonText}
isAlertVisible={_.size(errors) > 0 || Boolean(getErrorMessage()) || !_.isEmpty(props.formState.errorFields)}
isAlertVisible={_.size(errors) > 0 || Boolean(errorMessage) || !_.isEmpty(props.formState.errorFields)}
isLoading={props.formState.isLoading}
message={_.isEmpty(props.formState.errorFields) ? getErrorMessage() : null}
message={_.isEmpty(props.formState.errorFields) ? errorMessage : null}
onSubmit={submit}
footerContent={props.footerContent}
onFixTheErrorsLinkPressed={() => {
Expand Down Expand Up @@ -362,7 +372,7 @@ const Form = (props) => {
errors,
formContentRef,
formRef,
getErrorMessage,
errorMessage,
inputRefs,
inputValues,
submit,
Expand Down