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

main
Tsu-ba-me 2 years ago
parent ae07c8b66d
commit e5d7014b9f
  1. 10
      striker-ui/components/InputWithRef.tsx
  2. 4
      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,6 +167,14 @@ const InputWithRef = forwardRef(
[initOnFocus, inputTestBatch], [initOnFocus, inputTestBatch],
); );
/**
* Using any setState function synchronously in the render function
* directly will trigger the 'cannot update a component while readering a
* different component' warning. This can be solved by wrapping the
* setState call(s) in a useEffect hook because it executes **after** the
* render function completes.
*/
useEffect(() => {
if (isFirstRender) { if (isFirstRender) {
const isValid = const isValid =
testInput?.call(null, { testInput?.call(null, {
@ -175,6 +184,7 @@ const InputWithRef = forwardRef(
onFirstRender?.call(null, { isValid }); 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 = <
], ],
); );
useEffect(() => {
if (isFirstRender) { if (isFirstRender) {
buildInputFirstRenderFunction(INPUT_ID_UPS_TYPE)({ buildInputFirstRenderFunction(INPUT_ID_UPS_TYPE)({
isValid: Boolean(inputUpsTypeIdValue), isValid: Boolean(inputUpsTypeIdValue),
}); });
} }
}, [buildInputFirstRenderFunction, inputUpsTypeIdValue, isFirstRender]);
return content; return content;
}; };

Loading…
Cancel
Save