What use is there to nesting DndContext if events don't go higher than the first parent? #766
-
Hello there,
So from this situation, I would need a stucture like this:
But what good is this if all my events (onDragStart, onDragEnd) are stopped at Context 3 and will never reach Context 1 or 2 ? If I want events to be shared, I have to remove the second and third context ? But now I can't have different modifiers on the DndContexts and I can't have a DragOverlay on only one of the containers. Or am I missing something ? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Currently, the best way to do this is to have a single You can also use You can leverage the function DraggableItem() {
const {...} = useDraggable({
id,
data: {
modifiers: [restrictToVerticalAxis],
renderDragOverlay: ({active}) => <Item id={active.id} />,
},
});
}
function App() {
const [active, setActive] = useState(null);
return (
<DndContext
modifiers={active ? active.data.current.modifiers : []}
onDragStart={({active}) => {
setActive(active);
}}
onDragEnd={() => setActive(null)}
>
<DragOverlay>
{active?.data.current?.renderDragOverlay?.(active)}
</DragOverlay> In the future, there will likely be new APIs introduced to facilitate configuring |
Beta Was this translation helpful? Give feedback.
-
Is there any other way now to communicate across |
Beta Was this translation helpful? Give feedback.
Currently, the best way to do this is to have a single
<DndContext>
provider. You can dynamically change what modifiers are active or what theDragOverlay
renders based on the type of active element.You can also use
useDndMonitor()
to manage state locally rather than having to manage state at the top level of your application where<DndContext>
is rendered.You can leverage the
data
property ofuseDraggable
anduseSortable
to storemodifiers
that should be applied when that type of draggable item is active, and also pass a component that should be rendered for the DragOverlay.