Faster Paint with delayed flush of Effects #1435
Merged
+50
−46
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request enables faster paint of chat switch the changes to DND kit with the following changes:
Change 1: Changing
useIsoMorphicLayoutEffect
touseEffect
Reason: Changing state in a layout effect can delay browser paint.
Details: The DND kit mutates state in a layout effect, but only for registering elements as draggable. This hurts performance as changing state in a layout effect can delay browser paint. In this case, there is no need to change state in a layout effect because this state change is only registering draggable elements which can wait until browser paint.
Example: A CodeSandbox example demonstrates how setting state in a layout effect delays browser paint.
Case 1: Changing state in a layout effect.

Screenshot showing Paint after flush.
Case 2: Changing state in an effect.

Screenshot showing Paint before flush.
Change 2: Updating
useRect
anduseRects
Reason: Removed the reducer implementation in favor of state.
Details: Similar to the first point, the state was changing in a layout effect when a user clicks and drag a DND node. Initially, we considered replacing useIsoMorphicLayoutEffect with useEffect. However, to avoid visual discrepancies, we replaced the useReducer with useState. This change ensures that even though the state updates within a layout effect, the paint is delayed only when there is a meaningful state change. This is because useReducer doesn't bail out of meaningless state updates, whereas useState can.
Automated Tests:

We also ran Cypress and Unit Test on github repo to gain confidence on proposed changes. Here are the results