use effect and state to disable clear button: prevent hydration errors if the field is initialized differently in the client

This commit is contained in:
Riccardo Balbo 2024-10-24 16:21:37 +02:00
parent 820ea90267
commit 9c6e98b47b
1 changed files with 6 additions and 1 deletions

View File

@ -524,6 +524,11 @@ function InputInner ({
const remaining = maxLength && maxLength - (field.value || '').length
const [showClearButton, setShowClearButton] = useState(clear)
useEffect(() => {
setShowClearButton(clear && field.value)
}, [field.value])
return (
<>
<InputGroup hasValidation className={inputGroupClassName}>
@ -538,7 +543,7 @@ function InputInner ({
isInvalid={invalid}
isValid={showValid && meta.initialValue !== meta.value && meta.touched && !meta.error}
/>
{(clear && field.value) &&
{showClearButton &&
<Button
variant={null}
onClick={(e) => {