I want to change the style of the selected ListItem
as shown in this codesandbox.
In the sandbox I use a global css stylesheet, but I would like to use the withStyle technique.
How can I do it?
What className
should I use in order to target the selected ListItem?
Asked
Active
Viewed 2,708 times
1

NearHuscarl
- 66,950
- 18
- 261
- 230

Louis Coulet
- 3,663
- 1
- 21
- 39
1 Answers
2
If you are using stateless functional component, you should use makeStyles()
instead of withStyles()
.
withStyles()
is HOC which is often used with class-based component.makeStyles()
on the other hand is a hook creator, and hooks are more suitable in functional components. I'd advice you and your children to use this approach since more and more libraries are adopting hooks as the primary API instead of HOC.
You can see a list of ListItem
css classes and class keys here to know which class to apply to.
const useStyles = makeStyles({
root: {
"& .Mui-selected": {
backgroundColor: "pink",
color: "red",
fontWeight: "bold"
},
"& .Mui-selected:hover": {
backgroundColor: "tomato"
}
}
});
function App() {
const [selected, setSelected] = React.useState("home");
const styles = useStyles();
return (
<List className={styles.root}>
...
</List>
);
}
If you want to use class key (selected) instead of css class (Mui-selected), you can write it like this
const useStyles = makeStyles({
root: {
"&$selected": {
backgroundColor: "pink",
color: "red",
"&:hover": {
backgroundColor: "tomato"
}
}
},
selected: {}
});
And apply to the component like this
<List>
<ListItem
classes={{
root: styles.root,
selected: styles.selected
}}
...
>
...
</ListItem>
...
</List>
At this point you should consider refactor ListItem
to a separate component to clean up the duplicated code.
Live Example

NearHuscarl
- 66,950
- 18
- 261
- 230
-
Thank you for providing this detailed answer! I like how you describe makeStyles as the equivalent of withStyles. As a side note, you mention that withStyles only works for class-based, but I think it works fine with funtion components as well: https://codesandbox.io/s/63973501material-ui-and-reactjs-how-to-style-a-selected-listitem-using-withstyles-forked-f180y?file=/index.js – Louis Coulet Sep 20 '20 at 08:50
-
Also, how could we modify the styles in order to use "selected" instead of ".Mui-selected", as in the MenuItem example? [https://material-ui.com/customization/components/#pseudo-classes] – Louis Coulet Sep 20 '20 at 08:52
-
I see you even have added code for using "selected", you rock! I am not sure to understand completely: would you have pointers to documentation about "class key"? Also, why do you need the empty ```selected: {}```? – Louis Coulet Sep 20 '20 at 09:38
-
@LouisCoulet See this answer [here](https://stackoverflow.com/a/59251768/9449426) for more info. If you put your styles in `select` instead of `root`, the styles will be overrided due to css specificity. `selected: {}` is there to generate the appropriate class name for the `ListItem` when selected – NearHuscarl Sep 20 '20 at 10:15
-
Every Material-UI components have some specific css classes and class keys for you if you want to override certain parts of the UI. [Here](https://material-ui.com/api/list-item/#css) is the css classes (Global class column) and class keys (Rule name column) for the `ListItem` (already linked in my answer). – NearHuscarl Sep 20 '20 at 10:22