[styles] Dynamic makeStyles unexpected behavior
See original GitHub issue- This is not a v0.x issue.
- I have searched the issues of this repository and believe that this is not a duplicate.
I have found some issues using makeStyles with dynamic styles (arrow functions) and i’m going to list them here since i think those may be related, and i feel that is unnecessary to create one issue per each of these points.
This issues are shown in this codesandbox MCVE https://codesandbox.io/s/lrwvw5lx6q.
- The class name of a dynamic style contains 2 classes, the first is the represents the static class (hence is empty) and the second the dynamic class (with the actual styles). This behavior may be the intended to allow the reference of a static class from a dynamic class but i don’t think is working, at least not completely.
- Cannot mix or reference (with
$) a static class (not using arrow function) from a dynamic class (using arrow). - Using some complex classname (not sure exacly which ones cause this behavior) when the component is rerendered (by a state change for instance) the previous style is not removed and the style sheet gets polluted and starts to have duplicate classes.
- Cannot use a dynamic keyframe (using arrow function).
- Cannot use/reference a static keyframe (just as an object) from a dynamic class. This may be related to (2).
- And the last is more a question, i checked the code for
makeStylesand i see that it does not depend on props, everytimeuseStylesis called and no matter if the props are the exacly the same the stylesheet is updated. Any reason for this?
Because when you call useStyles(props) with the same props or even using something like this:
const {
height,
width,
...rest
} = props;
// using only props that affect the styles generated
const memoizedStyleProps = useMemo(() => ({ height, width }), [height, width]);
const classes = useStyles(memoizedStyleProps);
Doesn’t seem necessary to update the stylesheets.
Expected Behavior
- Just one class name per dynamic style, e.g.
makeStyles-animate-10 - Be able to reference a static class from a dynamic class
- Be able to rerender the component with complex classes without ending up with duplicate css rules in the style sheet.
- Be able to use a key frame as a result of an arrow function.
- Be able to use a static key frame from a dynamic style.
Current Behavior
- Two class names per dynamic style, e.g.
makeStyles-animate-4 makeStyles-animate-10 - Cannot reference a static class from a dynamic class. In the example shows
index.js:26 Warning: [JSS] Could not find the referenced rule simple in makeStyles. - Using a complex classname and rerendering the component causes the style sheet gets polluted
- Not able to use a key frame as a result of an arrow function.
- Not able to use a static key frame from a dynamic style. In the example shows
index.js:26 Warning: [JSS] Referenced keyframes rule "rotate1" is not defined.
Steps to Reproduce
- Inspect any of the elements using a dynamic class name and it will have a two classnames for.
- In the example, if you change the definition of the class
simplefromsimple: {}tosimple: () => ({})the titleHello CodeSandboxwill have the expected green color. - Click one of the buttons in the example multiple times and then either inspect the
h2containingYou clicked X times!or checking the rules in the sandboxdocument.styleSheetsthere will be multiple rule definitions repeated. - In the example the keyframe named
rotate2that is inside an arrow function is generated with empty body. The animation is not being applied. - In the example the keyframe named
rotate1is generated correctly but it is not referenced. The animation is not being applied.
Your Environment
| Tech | Version |
|---|---|
| Material-UI | v4.0.0-alpha.8 and v4.0.2 |
| React | 16.8.0 |
| Browser | Chrome: version 73.0.3683.103 (Build official) (64 bits) |
| TypeScript | No |
Not sure if all this points are really issues or some of them are not allowed and its behavior is undefined.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:31
- Comments:14 (7 by maintainers)
Top Results From Across the Web
[styles] Dynamic makeStyles unexpected behavior #15511
I have found some issues using makeStyles with dynamic styles (arrow functions) ... [styles] Dynamic makeStyles unexpected behavior #15511.
Read more >Dynamic makeStyles unexpected behavior #15511 - Issuehunt
I have found some issues using makeStyles with dynamic styles (arrow functions) and i'm going to list them here since i think those...
Read more >Adding dynamic style to material ui Menu with makeStyles not ...
I'm trying to dynamically generate classes on the material ui Menu this way : const useStyles = (props) => { return makeStyles({ paper: ......
Read more >Adding dynamic style to material ui Menu with makeStyles not ...
Coding example for the question Adding dynamic style to material ui Menu with makeStyles not working-Reactjs.
Read more >How To Style React Components | DigitalOcean
In this tutorial, you'll learn three different ways to style React components: plain Cascading Style Sheets (CSS), inline styles with ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

So, what’s the work around on v4?
Is there any update on this issue? Still getting more than one class when using props for adaptive styling.