I have 2 components with similar logic, they both conditionally display a message, based on the local state from useState hook and two props from the redux state. However, the problem occurs when I click on the ConnectedPasswordChange button - I see two messages, one from its own component and one from ProfileForm. Any idea how to get rid of that? Does it have something to do with event bubbling?
const ConnectedPasswordChange = (): Node => {
const [submitted, setSubmitted] = useState(false);
const isUpdating = useSelector(state => state.user.isUpdating);
const updateError = useSelector(state => state.user.updateError);
const handleSubmit = () => {
const { currentPassword, newPassword } = passwordDetails;
dispatch(passwordUpdateRequested(currentPassword, newPassword));
setSubmitted(true);
setTimeout(() => setSubmitted(false), 5000);
};
return (
<Form onSubmit={handleSubmit}>
<PasswordChange passwordDetails={passwordDetails} onPasswordChange={setPasswordDetails} />
<div style={{ textAlign: "center" }}>
<Form.Button secondary>Button</Form.Button>
</div>
{submitted && !isUpdating && !updateError && <Message positive content="Success" />}
</Form>
);
};
ConnectedPasswordChange is used in ProfileForm component that is using the same logic
const ProfileForm = ({ id, user: initialUser, locations, isUpdating, updateError, onSubmit }: Props): Node => {
const [submitted, setSubmitted] = useState(false);
const handleSubmit = () => {
onSubmit(id);
setSubmitted(true);
setTimeout(() => setSubmitted(false), 5000);
};
return (
<Segment className="my-profile-main-container">
<Title />
<Divider />
<Form onSubmit={handleSubmit}>
<PersonalDetails personalDetails={user} onPersonalDetailsChange={handleDetailsChange} />
<Divider />
<BankDetails bankDetails={user} onBankDetailsChange={handleDetailsChange} />
<div style={{ textAlign: "center" }}>
<Form.Button secondary>Button</Form.Button>
</div>
{submitted && !isUpdating && !updateError && <Message positive content={t("updateSucceeded")} />}
<Divider />
<ConnectedPasswordChange />
</Form>
</Segment>
);
};