I have a container that has two children: an input field and another div
element that conditionally renders depending on the value for isActive
. The problem that I'm running into is that whenever I click on the child div
, then onBlur
is triggered.
How can I prevent this from happening? I've already tried e.stopPropagation()
as you can see below. Also, I've tried moving onBlur
and onFocus
to the container div
, but that didn't work either.
import React from "react";
import "./styles.css";
export default function App() {
const [isActive, setIsActive] = React.useState(false);
const handleClick = React.useCallback((e) => {
e.stopPropagation(); // does not work
}, []);
return (
<div
className="container"
onFocus={() => setIsActive(true)}
onBlur={() => setIsActive(false)}
>
<input />
{isActive && <div className="child" onClick={handleClick} />}
</div>
);
}