-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Using the makeStyles / useStyles() pattern causes React apps to render twice #20313
Comments
This codesandboxes use StrictMode (see index.js) which calls render twice. Depending on your component tree this causes issues if you have side effects during render (such as console.log or
Not necessarily. What we care more in react are commits and even more is actual performance measurements. Render counting is a tool to identify issues but it does not necessarily imply you have a performance issue. |
It sounds like you're dismissing the issue. Maybe I misread your comment, but it seems you're saying that React I'd like to point out, a second time, that in
Is If so, this is intentional behavior. If not, this an issue. I'd like to humbly request that you mark this behavior ( Much ❤️ |
Rendering twice is the whole point of StrictMode. We can't and shouldn't do anything about it. It's like asking someLibraryMethod to not execute twice after the app-developer called it twice. makeStyles isn't forcing your component to render twice. StrictMode is. You can observe the same behavior with any other hook. It's not "my believe" that this is fine. This is by design of React. If you think rendering twice is a problem in StrictMode then the react repo would be more appropriate for discussion. Both sandboxes render twice. It's hidden in withStyles though in the second sandbox. |
@NawarA Proof: https://codesandbox.io/s/strictmode-w-and-wo-hooks-vex1m deployed in production: https://csb-vex1m.netlify.com/. |
You're right! Thanks dude! You're awesome 💪 |
Let me start by saying, I ❤️ MUI. K, lets dive in:
I log how frequently my react app renders, just so I maintain fine grain control over DOM renders / reflows. I noticed whenever I start my react app, it rendered twice at the root level. I began investigating the root cause and I've narrowed it down to:
This causes react apps to render twice. Here's the thing, on the first render,
classes
is fully populated and ready to go. The second render returns the sameclasses
object, with no differences --> thus the second re-render is unneeded and wasteful.Current Behavior 😯
See above ^
Expected Behavior 🤔
const classes = useStyles()
does not effect component re-rendering.Using
withStyles
instead ofmakeStyles
(which have the same outcome of making aclasses
object available), DOES NOT cause the component to render twice. It's expected themakeStyles
andwithStyles
do not effect rendering.withStyles
is working that way,makeStyles
is not.Steps to Reproduce 🕹
See the codesandbox links below for evidence of the issue:
makeStyles renders twice
withStyles renders once
As you can see,
withStyles
has the correct behavior, andmakeStyles
, has the buggy behavior.Context 🔦
Rendering apps twice costs:
Ask yourself, if you were Captain Picard, what would you do in this situation? 😆
Your Environment 🌎
The text was updated successfully, but these errors were encountered: