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.

Using the makeStyles / useStyles() pattern causes React apps to render twice

See original GitHub issue

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:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";

export default ()=> {
 const classes = useStyles();
};

const useStyles = makeStyles(()=> { ... });

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 same classes 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 of makeStyles (which have the same outcome of making a classes object available), DOES NOT cause the component to render twice. It’s expected the makeStyles and withStyles 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, and makeStyles, has the buggy behavior.

Context 🔦

Rendering apps twice costs:

  • money
  • wastes user resources
  • wastes planetary resources

Ask yourself, if you were Captain Picard, what would you do in this situation? 😆

Your Environment 🌎

Tech Version
Material-UI v4.9.8
React 16.13
Browser Chrome 80

Issue Analytics

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

github_iconTop GitHub Comments

8reactions
oliviertassinaricommented, Mar 29, 2020

@NawarA ⚠️ This double rendering is a development only behavior. It won’t happen in production.

Proof: https://codesandbox.io/s/strictmode-w-and-wo-hooks-vex1m deployed in production: https://csb-vex1m.netlify.com/.

1reaction
eps1loncommented, Mar 28, 2020

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 ++renderCount)

Rendering apps twice costs:

money wastes user resources wastes planetary resources

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the makeStyles / useStyles() pattern causes React apps ...
This causes react apps to render twice. Here's the thing, on the first render, classes is fully populated and ready to go. The...
Read more >
Why does react re-render twice when using the state hook ...
It seems the offending code comes from <React.StrictMode> ReactDOM.render( <React.StrictMode> <App /> </React.StrictMode>, document.
Read more >
Styling in React: 5 ways to style React apps - LogRocket Blog
In this article, we will review styling React components with inline styling, styled-components, CSS modules, Tailwind CSS, and Sass and CSS ...
Read more >
React Components rendered twice — any way to fix this?
A) Functional Component with useState. function App() { const [click, setClick] = React.useState(0);console.log('I render ', click); return (
Read more >
JSS integration with React
React -JSS integrates JSS with React using the new Hooks API. ... Critical CSS extraction - only CSS from rendered components gets extracted....
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