question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

classes prop needs to be memoized

See original GitHub issue

Maybe I am interpreting this wrong, but I believe that if I pass a classes prop to a component wrapped with withStyles, that component always re-renders with the parent, even if no props change.

const myClasses = { test: 'myClass' };

class MyComponent extends React.PureComponent {
  render() {
    return (
      <MySecondComponent classes={myClasses} />
    );
  }
}

I believe what’s happening is when MySecondComponent is checked for updates, withStyles generates a new object despite the fact that its classes prop has not changed. https://github.com/mui-org/material-ui/blob/v1-beta/packages/material-ui/src/styles/withStyles.js#L234

This causes the SecondComponent to always re-render, despite its props not changing.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
oliviertassinaricommented, Apr 26, 2018

In the future, we could also try to memoize the classes when users are providing a classes property.

Yep: #8142

0reactions
CharlesStovercommented, May 1, 2018

PR open, sorry for the delay. I had trouble getting yarn build to work on my machine, so I just used babel-es2015 stage-0 react and replaced the node_module on my machine to test. Worked great. I think it only added ~600 bytes for an uncompressed transpile.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mui/material-ui - classes prop needs to be memoized - GitHub
I believe what's happening is when MySecondComponent is checked for updates, withStyles generates a new object despite the fact that its classes ......
Read more >
How to Implement Memoization in React to Improve ... - SitePoint
Memoization is a good technique for improving performance in React apps by avoiding unnecessary re-renders of a component if its props or state ......
Read more >
Memoization in React js - Topcoder
Memoization Using Memo​​ Memo allows us to implement memoization in functional components since PureComponents can only be used in class ...
Read more >
How to Memoize Components in React | by Ross Bulat - Medium
We will cover how to deal with callback functions as props (state included) further down with the useCallback() Hook.
Read more >
What is Memoization? How and When to ... - freeCodeCamp
We also have a child component to which we're passing a prop name which is a string. import React from "react" import Child...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found