I have a component that I reuse most of its logic. I'm looking to avoid re-renders on its children components which happen every time I hover over the parent:
const ReusableComponent = ({ conditional }) => {
const [isHovered, setIsHovered] = useState(false);
const AnotherReusableComponent = ({ children }) => (
<div>{children}</div>
);
const renderComponent = () => {
if (conditional) {
return <ComponentA />;
};
return <ComponentB />;
};
return (
<div>
<title
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
Menu
</title>
<div className={isHovered ? 'oneClass' : 'otherClass'}>
<AnotherReusableComponent>
{renderComponent()}
</AnotherReusableComponent>
</div>
</div>
);
}
Notes:
ComponentA
andComponentB
are shown/hidden depending on classNameonHover
event toggles className- Tried
memo
, didn't work. - The re-render happens
onHover