Conversation
|
Size Change: +80 B (0%) Total Size: 1.42 MB
ℹ️ View Unchanged
|
|
There may also be a way to fix this in I don't mind looking at that as a separate task. |
| targetRootClientId, | ||
| targetBlockIndex, | ||
| insertBlocks | ||
| onDrop: useCallback( |
There was a problem hiding this comment.
If I'm understanding properly the purpose of the PR, I think we can avoid the useCallback entirely and avoid having the callback as a dependency. Instead we can just keep the latest value of the callback in a ref and call the ref in the callback.
We use this technique in several hooks already and I believe @ellatrix built a reusable hook somewhere to keep the ref "fresh" for us, right?
There was a problem hiding this comment.
We do this in several places, yes, but I don't think we have a utility function for it yet.
| * | ||
| * @param {string} targetRootClientId The root client id where the block(s) will be inserted. | ||
| * @param {number} targetBlockIndex The index where the block(s) will be inserted. | ||
| * @param {Object} dropEventData Block drop event data. |
There was a problem hiding this comment.
Is there any way to make this type more specific?
| * A function that returns an event handler function for block-related file drop events. | ||
| * An event handler function for block-related file drop events. | ||
| * | ||
| * @param {Array} files The files array from the drop event. |
There was a problem hiding this comment.
Here the same - if it is possible, I think it would be better to add a more specific type
|
Thanks - I planned to give that a review today, but will probably be tomorrow now. |
|
Closing as this has been fixed by #30310 |
Description
useDropZonecan end up in a constant cycle of adding and removing dropzone callbacks if the event handlers passed to it aren't stable. It uses and effect for adding and removing, with dependencies on the callbacks:gutenberg/packages/components/src/drop-zone/index.js
Lines 30 to 53 in c62ccd8
This seems to have been a problem for a while with the callbacks returned from
useOnBlockDrop, which doesn't useuseCallbackand even if it did, the callbacks would still be unstable every time thetargetRootClientIdandtargetBlockIndexchange while dragging to different areas on a page:gutenberg/packages/block-editor/src/components/use-on-block-drop/index.js
Lines 243 to 264 in c62ccd8
The change in this PR makes it so that the callbacks returned from
useOnBlockDropare stable by using minimising what the callbacks it returns do. The callbacks now set state for the data returned from the drop event, and a separate effect handles the actual dropping.This may lead to some performance benefits, but I think it mainly solves and issue where sometimes the dropzone target indicator can flicker between two separate dropzones (e.g. if there's another drop zone for nested blocks).
How has this been tested?
Not sure what options there are for automated testing for this kind of change
To reproduce the original issue
dropZones.add(https://github.com/WordPress/gutenberg/blob/trunk/packages/components/src/drop-zone/index.js#L41) anddropZones.remove(https://github.com/WordPress/gutenberg/blob/trunk/packages/components/src/drop-zone/index.js#L43).In this branch: In this branch the console logs happen infrequently.
In
trunk: console logging happens lots, even when not dragging and dropping 😬Types of changes
Bug fix (non-breaking change which fixes an issue)
Checklist: