fix(striker-ui): run 'set validity on first render' in useEffect

main
Tsu-ba-me 2 years ago
parent ae07c8b66d
commit e5d7014b9f
  1. 28
      striker-ui/components/InputWithRef.tsx
  2. 14
      striker-ui/components/ManageUps/AddUpsInputGroup.tsx

@ -5,6 +5,7 @@ import {
forwardRef, forwardRef,
ReactElement, ReactElement,
useCallback, useCallback,
useEffect,
useImperativeHandle, useImperativeHandle,
useMemo, useMemo,
useState, useState,
@ -166,15 +167,24 @@ const InputWithRef = forwardRef(
[initOnFocus, inputTestBatch], [initOnFocus, inputTestBatch],
); );
if (isFirstRender) { /**
const isValid = * Using any setState function synchronously in the render function
testInput?.call(null, { * directly will trigger the 'cannot update a component while readering a
inputs: { [INPUT_TEST_ID]: { value: inputValue } }, * different component' warning. This can be solved by wrapping the
isIgnoreOnCallbacks: true, * setState call(s) in a useEffect hook because it executes **after** the
}) ?? false; * render function completes.
*/
onFirstRender?.call(null, { isValid }); useEffect(() => {
} if (isFirstRender) {
const isValid =
testInput?.call(null, {
inputs: { [INPUT_TEST_ID]: { value: inputValue } },
isIgnoreOnCallbacks: true,
}) ?? false;
onFirstRender?.call(null, { isValid });
}
}, [input.props.id, inputValue, isFirstRender, onFirstRender, testInput]);
useImperativeHandle( useImperativeHandle(
ref, ref,

@ -1,4 +1,4 @@
import { ReactElement, ReactNode, useMemo, useState } from 'react'; import { ReactElement, ReactNode, useEffect, useMemo, useState } from 'react';
import { BLACK } from '../../lib/consts/DEFAULT_THEME'; import { BLACK } from '../../lib/consts/DEFAULT_THEME';
@ -142,11 +142,13 @@ const AddUpsInputGroup = <
], ],
); );
if (isFirstRender) { useEffect(() => {
buildInputFirstRenderFunction(INPUT_ID_UPS_TYPE)({ if (isFirstRender) {
isValid: Boolean(inputUpsTypeIdValue), buildInputFirstRenderFunction(INPUT_ID_UPS_TYPE)({
}); isValid: Boolean(inputUpsTypeIdValue),
} });
}
}, [buildInputFirstRenderFunction, inputUpsTypeIdValue, isFirstRender]);
return content; return content;
}; };

Loading…
Cancel
Save