Use of WithStyles forces "classes" attribute to be present in TypeScript [has workaround]
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.
Current Behavior 😯
When using withStyles
to add style to a component, and WithStyles<typeof styles>
to describe the props for the component, the user of the component is forced to provide the classes
prop otherwise they get a type error.
Expected Behavior 🤔
The user of my component should not be required to provide a classes
prop.
Steps to Reproduce 🕹
No repro to share yet, but looks like similar to https://github.com/mui-org/material-ui/issues/10022.
It isn’t clear what to do to make classes
optional. Seems like having to add a type for it in my props definition is too complicated (how am I supposed to know how to type it? I want to leave that to material-ui)
Unless I missed it, the docs don’t explain this part.
Your Environment 🌎
Tech | Version |
---|---|
Material-UI | v3.5.1 |
React | |
Browser | |
TypeScript | |
etc. |
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Use of WithStyles forces "classes" attribute to be present in ...
When using withStyles to add style to a component, and WithStyles<typeof styles> to describe the props for the component, the user of the ......
Read more >Can't use a react component with material-ui style classes in ...
withStyles accepts an object of type CSSProperties as parameter, not a string. makeStyles returns a className. Good luck, and please do tell me ......
Read more >Handbook - Interfaces - TypeScript
Notice that our object actually has more properties than this, but the compiler only checks that at least the ones required are present...
Read more >Cascade, specificity, and inheritance - Learn web development
Specificity is the algorithm that the browser uses to decide ... Attribute selectors and pseudo-classes have the same weight as a class.
Read more >TypeScript - Material-UI
Unfortunately due to a current limitation of TypeScript decorators, withStyles(styles) can't be used as a decorator in TypeScript. Customization of Theme. When ...
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
Because that
prop
is injected bywithStyles
.WithStyles
should only be used for components that are intended to be wrapped inwithStyles
.I faced this issue running this code:
See how |styles| and |Props| make a circular dependency. The error message is confusing since the problem is irrelevant to |classes| field.
Solution If you face this issue do NOT think that something is wrong with Material-UI/Typescript/React, most likely there is some crazy shit in your code. In my case the solution is changing
background: (props: Props)
tobackground: (props: {example:boolean})