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.
$
) a static class (not using arrow function) from a dynamic class (using arrow).makeStyles
and i see that it does not depend on props, everytime useStyles
is 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.
makeStyles-animate-10
makeStyles-animate-4 makeStyles-animate-10
index.js:26 Warning: [JSS] Could not find the referenced rule simple in makeStyles.
index.js:26 Warning: [JSS] Referenced keyframes rule "rotate1" is not defined.
simple
from simple: {}
to simple: () => ({})
the title Hello CodeSandbox
will have the expected green color.h2
containing You clicked X times!
or checking the rules in the sandbox document.styleSheets
there will be multiple rule definitions repeated.rotate2
that is inside an arrow function is generated with empty body. The animation is not being applied.rotate1
is generated correctly but it is not referenced. The animation is not being applied.| 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.
cc @kof
Any news on this?
Same here is called too many times unnecessary
Encountered this problem as well.
I noticed that this problem only occurs when using a nested selector. So adding a separate class to the element the nested selector points at and adding the dynamic styling to that class should solve the problem.
In this code sandbox I debugged the problem:
https://codesandbox.io/s/styling-duplicates-on-state-change-0weti
_Note: In my browser (FireFox) I had to close and open the inspector to see the affect, thus make sure you follow the debug instructions in the demo._
This doesn't work:
myParentElement: (props) => ({
'& .myNestedElement': {
// use props
}
})
This does work:
```
myNestedElement: (props) => ({
// use props
})
Is there any update on this issue? Still getting more than one class when using props for adaptive styling.
Also interested in item 6 – makeStyles
shouldn't re-calculate and re-attach styles unless the props change.
I'd imagine it can be fixed with a small change like https://github.com/schnerd/material-ui/commit/21f98bd61c6ab10bc54bf8195e70cefcc96502bb.
These issues should have been solved in v5 thanks to #22342 and the new @material-ui/styled-engine
package. You can play with it at: https://codesandbox.io/s/bold-goodall-2iltv?file=/src/App.tsx. By default, it wraps emotion.
Most helpful comment
Is there any update on this issue? Still getting more than one class when using props for adaptive styling.