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.
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DataGrid] Make possible to memoize rows and cells #7846
[DataGrid] Make possible to memoize rows and cells #7846
Changes from 1 commit
0562193
3065d0f
29aa143
7dd4395
b0fdc7e
c93175b
437879e
02acd1f
34975ee
1381e47
c5d1625
c342db6
3fea0a0
221be7d
7b899d1
c083ab1
723a823
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To override the row hover background color, otherwise it might look like the row wasn't rerendered
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it's still not enough -
&&&
is needed to actually override the hover colorThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't understand. The row doesn't appear to be rendering again on hover.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I managed to reproduce the bug, it's tricky to see it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it expected that the whole row is rerendered on cell focus change?
Screen.Recording.2023-02-20.at.19.52.49.mov
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, because which cell is focused comes from the
focusedCell
prop. If I remove this prop and pass it to the cell, from inside the row, then the row won't re-render because no prop has changed, even though the props passed to the cell will have changed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Side note: This seems inconsistent with the other components' names that do not change regardless of the package used (one exception is
DataGrid
/DataGridPro
/DataGridPremium
).Should we rename it to
GridColumnHeaders
for all packages?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't add a "before" demo because the comparison would be too ridiculous but here's https://codesandbox.io/s/crimson-tdd-fb0vec?file=/demo.tsx. Basically, in any click everything re-renders.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we memoize the
Cell
slot though?renderCell
is called inGridRow
component and the returned value is passed to theGridCell
.So if there's
renderCell
in the column - the cell will always rerender - see this demo: https://codesandbox.io/s/dreamy-roentgen-px945xThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, I added
React.memo
to the cell component by default now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this more, wouldn't it make more sense to call
renderCell
inside theGridCell
component?This should allow memoizing the
GridRow
by default (as opposed toGridCell
), and then theGridCell
can be memoized by users. (Maybe then we could go a step further and add a check inReact.memo
to memoizeGridCell
by default ifcolDef.renderCell
is defined?).If it's too time-consuming, we can work on this later - this PR is already a great improvement!
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we add this check to
React.memo
we lose the default shallow comparison function. I don't know what's the best approach here. I propose to leave this as it is and see the implications.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way, is there a way to reduce rerenders of the cells that have
renderCell
? Would memoizing the component returned byrenderCell
work?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested and it didn't work. To memoize cells with custom renderers, we should call
renderCell
and cache its content, then invalidate the cached value if some prop changes. If we use therow
prop as cache key, we fall again into the problem of the cell renderer depending on a selector. It's better to leave this for the user to cache the value. We can do a follow-up explaining how to do this.