Please see this codesandbox.
I have a parent div that needs to handle for draggable events (such as when a file is dragged over it). When onDragEnter
is invoked, I want the background color to change. However, I have a child div, which, when hovering over it, invokes the parent div's onDragLeave
event. I've tried using a ref with this child div to determine if the event target is contained in the child div but that doesn't seem to be working. How can I avoid a child div in React invoking the onDragLeave
event? Thanks!
import React, { useState } from 'react';
const App = () => {
const [dragOver, setDragOver] = useState(false);
const preventDefaults = (event) => {
event.preventDefault();
event.stopPropagation();
};
const onDragEnter = (event) => {
preventDefaults(event);
console.log('ENTER');
if (!dragOver) {
setDragOver(true);
}
};
const onDragLeave = (event) => {
preventDefaults(event);
console.log('LEAVE');
setDragOver(false);
};
return (
<div
data-testid="document-upload"
onDragEnter={onDragEnter}
onDragLeave={onDragLeave}
style={{
background: dragOver ? 'green' : 'red',
height: '20rem',
width: '20rem',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
<div
style={{ border: '2px solid blue', height: '10rem', width: '10rem' }}
/>
</div>
);
};
export default App;