question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useDndMonitor() hook always re-renders the component instead of firing callbacks

See original GitHub issue

Doc says

The `useDndMonitor` hook can be used within components wrapped in a DndContext provider to monitor the different drag and drop events that happen for that DndContext

Example:

  useDndMonitor({
    onDrop: () => {
      // this callback will get call when the drop occurs
    }
  });

From the above signature, My understanding was that, onDrop will be called when user drop item on the droppable.

But it re-renders the component on each mouse move. It should not re-renders the component. It should only call the callback.

Here is demo Link https://codesandbox.io/s/dndkit-issue-demo-m5xc7j?file=/src/App.js

This makes this hook un-usable since it re-renders always regardless of an event i wish to get notify.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
saravanan10393commented, Mar 2, 2022

@clauderic i have solved the issue outside of this package using the principle of use-context-selector package. Here is the code:

https://codesandbox.io/s/dndkit-issue-demo-m5xc7j?file=/src/dndmonitor.js

if this is fine, we can change the MonitorContext inside the src code as subscribe/notify.

1reaction
claudericcommented, Feb 28, 2022

Can the Monitor logic can be implemented using some sort of event emitting method instead of relaying on the react context?

This would be ideal yes, as it would also be synchronous rather than the current asynchronous approach. Unfortunately this isn’t something I have time to into at the moment as there are higher priority issues for me to focus on. If this is something you need, I would gladly accept a PR where we can discuss this further.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Hooks - Understanding Component Re-renders - Medium
This post is a result of my explorations to understand how each built-in hook influences the component re-renders/lifecycle.
Read more >
React Hooks useCallback causes child to re-render
So how would I go about making this onClick function within a functional component without having the children re-render? Is it at all...
Read more >
Hooks API Reference - React
It accepts a new state value and enqueues a re-render of the component. ... re-renders, the first value returned by useState will always...
Read more >
5 Ways to Avoid React Component Re-Renderings
1. Memoization using useMemo() and UseCallback() Hooks. Memoization enables your code to re-render components only if there's a change in the props. With...
Read more >
Understanding common frustrations with React Hooks
“React Hook cannot be called inside a callback”; “React Hooks must be called in a React function component or a custom React Hook...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found