React Material-UI withStyles样式注入失效问题及方案咨询
Hey there! Let's break down your questions step by step—first fixing the missing styles, then diving into whether withStyles is the right fit and if it causes CSS duplication.
Why Aren't Your Styles Showing Up?
Looking at your code, there's one critical issue preventing your styles from working, plus a small cleanup point:
Incorrect color value usage
You importedredfrom@material-ui/core/colors, butredis an object containing multiple shade values (likered[500],red[900]), not a direct color string. When you setbackgroundColor: red, the browser sees[object Object]instead of a valid color, so the style doesn't apply.Unnecessary props in render method
React class componentrendermethods don't receivepropsas an argument—you should access props viathis.propsinstead. Your currentrender(props)doesn't break the code, but it's misleading and unnecessary.
Fixed Code Example
import React, { Component } from 'react'; import Table from '@material-ui/core/Table'; import TableBody from '@material-ui/core/TableBody'; import TableCell from '@material-ui/core/TableCell'; import TableHead from '@material-ui/core/TableHead'; import TableRow from '@material-ui/core/TableRow'; import Paper from '@material-ui/core/Paper'; import { withStyles } from '@material-ui/core/styles'; import { red } from '@material-ui/core/colors'; // Styles const styles = theme => ({ root: { width: '100%', marginTop: theme.spacing.unit * 3, overflowX: 'auto', backgroundColor: red[500], // Use a specific shade of red }, table: { minWidth: 700, }, row: { '&:nth-of-type(odd)': { backgroundColor: theme.palette.background.default, }, }, }); class UserDataTable extends Component { // Remove props from render method render() { const { classes } = this.props; return ( <div id="UserDataTable"> <Paper className={classes.root}> <Table className={classes.table}> <TableHead> <TableRow> <TableCell>F</TableCell> <TableCell>j</TableCell> <TableCell>S</TableCell> <TableCell>p</TableCell> </TableRow> </TableHead> <TableBody> <TableRow> <TableCell>1</TableCell> <TableCell>2</TableCell> <TableCell>3</TableCell> <TableCell>4</TableCell> </TableRow> </TableBody> </Table> </Paper> </div> ) } } export default withStyles(styles)(UserDataTable);
Is withStyles the Optimal Approach?
It depends on your project setup:
- If you're using Material-UI v4 (which your imports suggest) and working with class components,
withStylesis absolutely a valid, officially supported approach. It's great for keeping styles tightly coupled to your component, accessing the theme, and avoiding global style conflicts. - If you're using function components,
makeStyles(also from@material-ui/core/styles) is more idiomatic. - For Material-UI v5, the official recommendation has shifted to using styled components (via Emotion or styled-components), but
withStylesstill works via the@mui/stylescompatibility layer if you're migrating from v4.
That said, withStyles does solve your goal of avoiding a massive single CSS file—styles are injected on demand when the component is rendered, which is efficient.
Will Importing This Component Cause CSS Duplication?
Nope! Material-UI's withStyles (and similar solutions like makeStyles) generates unique, scoped class names for your styles. Even if you import and render this component multiple times (or alongside other styled components), the CSS for your styles object will only be injected into the DOM once. The class names are reused across all instances of your component, so you won't have duplicated CSS bloat.
内容的提问来源于stack exchange,提问作者Peace

