Skip to content
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

fix(datagrid): add useMemo() calls #5949

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

wkeese
Copy link
Contributor

@wkeese wkeese commented Aug 27, 2024

Closes #5908, refs #5930.

Add useMemo() wrappers to useRowRenderer() and useSelectRows() to avoid needless Datagrid rerenders.

What did you change?

Added useMemo() wrappers in useRowRenderer() and useSelectRows() to avoid resetting datagridState.rows unnecessarily.

How did you test and verify your work?

Ran storybook examples and tested in my own app.

I suggest diffing ignoring whitespace.

Add useMemo() wrappers to useRowRenderer() and useSelectRows()
to avoid needless Datagrid rerenders.

Fixes carbon-design-system#5908, refs carbon-design-system#5930.
@wkeese wkeese requested a review from a team as a code owner August 27, 2024 03:05
@wkeese wkeese requested review from matthewgallo and devadula-nandan and removed request for a team August 27, 2024 03:05
Copy link

netlify bot commented Aug 27, 2024

Deploy Preview for carbon-for-ibm-products ready!

Name Link
🔨 Latest commit cd7addd
🔍 Latest deploy log https://app.netlify.com/sites/carbon-for-ibm-products/deploys/6703ee8f6cdb06000873f831
😎 Deploy Preview https://deploy-preview-5949--carbon-for-ibm-products.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@devadula-nandan
Copy link
Contributor

devadula-nandan commented Aug 29, 2024

Hi @wkeese,

I observe that these changes prevents the re-updation of rows[], but aren't fixing the re-rendering of the rows, which was the main objective of the issue if I am right.
pls find the recording below, and correct me if I got the reduced use case reproduction in the story wrong.

Screen.Recording.2024-08-29.at.6.47.12.PM.mov

@devadula-nandan
Copy link
Contributor

I suspect the root cause of this is the mentioned props, might not have memoization.

Screenshot 2024-08-29 at 9 15 25 PM

@wkeese
Copy link
Contributor Author

wkeese commented Aug 29, 2024

Hi @devadula-nandan. Thanks for looking at this! I think my PR is just one step on a long road to fixing the problem.

I didn't know about the problem with callbacks. Can we fix it with useCallback()? Or if that's impossible due to closure variables like datagridState, then useEventCallback()?

I thought there was another issue too though, with the allColumns variable, which is much harder to memoize. Strangely it didn't show up in your trace.

I've never used that react profiling tab. Is that what told you about the callback props changing?

@devadula-nandan
Copy link
Contributor

Update: I see the issue is with the root constantly rendering.

As you can observe In my reduced case the time state in SelectableRow (root in your case) changes every second, which triggers the re-rendering of component (parent), which in turn triggers the re-rendering of the datagrid(child) which is the expected behaviour in react, so the entire datagrid re-renders in this case, which was also observed in the profiling,
One way to avoid this is to seperate whatever logic is causing the re-rendering in the root, into its own component and contain the state changes within it, if it's feasible in your case. So the datagrid wont be effected.i have checked this in my local and I will be providing a codesandbox Demonstrating this.

@wkeese
Copy link
Contributor Author

wkeese commented Aug 30, 2024

Well, the code you're talking about was just to make a simple test case. The actual culprit, for unknown reasons, is useColumnRightAlign().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Datagrid: useRowRenderer() makes grid constantly rerender
3 participants