Repository navigation
Conversation
|
@bdumitrua is attempting to deploy a commit to the Ryan Gregory's projects Team on Vercel. A member of the Team first needs to authorize it. |
6 of 9 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Today every update re-renders the whole board: a new position, an unrelated re-render of the parent, or a drag. This PR makes the board re-render only the squares that changed.
This is part 1 of 2. You can merge them one after the other, or skip this one and merge only #243, which contains this commit plus the second change.
The public API does not change: the same exports, and
useChessboardContext()returns the same object as before.How it works
One context becomes three (
ChessboardProvider.tsx).ChessboardSettingsContextholds options and callbacks and changes rarely.ChessboardLiveContextholds the position, the dragged piece and arrows in progress, and changes on every move.ChessboardDragContextholdsallowDraggingandcanDragPiece.Each component reads only what it needs, so a move no longer re-renders every piece and square through the context.
useChessboardContext()merges the three, so code outside the library sees the same object as before.Squares get plain props and are memoized (
Board.tsx,Droppable.tsx,Square.tsx).Droppableused to take a render-prop child, which is a new function on every render, somemoonSquarenever helped.Boardnow computes the few values each square needs and passes them as props: the piece type, whether the piece is the drag ghost, and where it animates to.DroppableandSquareare memoized, so React skips the squares whose props did not change.PiecegetsghostandmoveToas props instead of readingdraggingPieceandpositionDifferencesfrom context.Event handlers read live state at event time.
Square, and the drag handlers in the provider, read the current position from a ref (liveRef) when the event fires, instead of subscribing to it during render.onMouseOverSquarestill fires during a drag.Stable defaults. The default
squareStyles = {},arrows = [],boardStyleand the sensor options were new objects on every render, which changed the context each time. They are memoized now.Tap detection on touch screens (
Square.tsx).isClickingOnMobilestate needed a re-render on every touch and an effect that watcheddraggingPiece.touchstartrecords the counter, andtouchendfires the click only if no drag started in between. The behavior is the same.squareRendererreceives the piece object only when a renderer is set, so squares without one do not depend on it.About the size: a large part of the
ChessboardProvider.tsxdiff is the dependency list of the memoized settings object and re-indentation from the nested providers.docs/stories/performance/RenderCounts.stories.tsx(273 lines) is a Storybook story that counts renders per scenario. It is docs only; I can drop it if you'd rather not keep it.Type of change
How Has This Been Tested?
pnpm test:staticandpnpm buildpass.Median work per update on an 8×8 board, stock 5.12.4 → this PR, in ms:
These numbers come from a bare page with just the board. In a real app the main thread is also busy with the rest of the UI, state updates and often a running chess engine (sometimes more than one), so I'd expect the absolute savings there to be larger. The gain also grows with board size; #243 has 16×16 numbers for both changes together.
This PR alone does not help with inline callbacks or changing
squareStyles; #243 covers those. Dragging is dominated by dnd-kit's own work and stays about the same.Checklist: