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
Show file tree
Hide file tree
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
6 changes: 1 addition & 5 deletions src/components/CheckboxWithLabel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState, useEffect} from 'react';
import React, {useState} from 'react';
import PropTypes from 'prop-types';
import {View} from 'react-native';
import _ from 'underscore';
Expand Down Expand Up @@ -89,10 +89,6 @@ const CheckboxWithLabel = (props) => {
setIsChecked(newState);
};

useEffect(() => {
setIsChecked(props.isChecked);
}, [props.isChecked]);

const LabelComponent = props.LabelComponent;

return (
Expand Down
24 changes: 17 additions & 7 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 @@ -95,6 +95,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 @@ -146,12 +147,21 @@ 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 : '');
return typeof latestErrorMessage === 'string' ? latestErrorMessage : '';
}, [props.formState]);

/**
Expand Down Expand Up @@ -325,9 +335,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 @@ -363,7 +373,7 @@ const Form = (props) => {
errors,
formContentRef,
formRef,
getErrorMessage,
errorMessage,
inputRefs,
inputValues,
submit,
Expand Down
2 changes: 0 additions & 2 deletions src/pages/EnablePayments/TermsStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ function TermsStep(props) {
<LongTermsForm />
<CheckboxWithLabel
style={[styles.mb4, styles.mt4]}
isChecked={hasAcceptedDisclosure}
onInputChange={toggleDisclosure}
LabelComponent={() => (
<Text>
Expand All @@ -73,7 +72,6 @@ function TermsStep(props) {
)}
/>
<CheckboxWithLabel
isChecked={hasAcceptedPrivacyPolicyAndWalletAgreement}
onInputChange={togglePrivacyPolicy}
LabelComponent={() => (
<Text>
Expand Down