I have a simple functional component which toggles the password field to and from visible via a small button which changes between closed and opened eye images.
There is no state affected in the parent, however, the parent un-mounts and re-mounts a split second after the image changes and this resets the child's isVisible state to false every time. Below is the code:
const Password = (props) => {
const [isVisible, setIsVisible] = useState(false);
const handleIsVisible = useCallback(() => setIsVisible(prev => !prev), []);
return(
<>
<p className="EditProfileFieldLabel SubHeading">
{(props.name === "password") ? "Password" : "Confirm Password"}
</p>
<div className="PasswordVisibleContainer">
<button
className="PasswordVisibleBtn"
onClick={handleIsVisible}
>
<img
src={(isVisible) ? passShowImg : passHideImg}
alt="Visible"
/>
</button>
</div>
<input
className="Field LightGreenBG EditProfileFieldLong"
name={props.name}
placeholder="**********"
onChange={props.handleOnChange}
type={(isVisible) ? "text" : "password"}
/>
</>
);
}
Can anyone see something obvious that I'm missing? I don't think the parent should be re-mounting/re-rendering because of a child state change.