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.

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:closed
  • Created 5 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eps1loncommented, Jan 29, 2019

But my question is, why does material-ui’s WithStyles force the classes prop?

Because that prop is injected by withStyles. WithStyles should only be used for components that are intended to be wrapped in withStyles.

0reactions
Romex91commented, Dec 18, 2020

I faced this issue running this code:

import React from "react";
import { withStyles, createStyles, WithStyles, Theme } from "@material-ui/core";

const styles = (theme: Theme) =>
  createStyles({
    scrollableContainer: {
      background: (props: Props) => (props.example ? "black" : "blue"),
    },
  });

interface Props extends WithStyles<typeof styles> {
  example: boolean;
}

const Demo = withStyles(styles)(
  class extends React.PureComponent<Props> {
    render() {
      return (
        <div className={this.props.classes.scrollableContainer}>dadadadada</div>
      );
    }
  }
);

export default Demo;


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) to background: (props: {example:boolean})

Read more comments on GitHub >

github_iconTop 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 >

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