Your component style may depend on the props and state of the components:
constuseStyles=makeStyles<{ color:string; }>()( (_theme, { color }) => ({"root": {"backgroundColor": color } }));//...const { classes } =useStyles({ "color":"grey" });
...Or it may not:
constuseStyles=makeStyles()({//If you don't need neither the theme nor any state or//props to describe your component style you can pass-in//an object instead of a callback."root": {"backgroundColor":"pink" }});//...const { classes } =useStyles();
constuseStyles=makeStyles({ "name":"MyComponent" })({"root": {/*...*/ }});//...const { classes } =useStyles();//classes.root will be a string like: "tss-xxxxxx-MyComponent-root"
Usually, you want the name to match the name of the component you are styling. You can pass the name as the first key or a wrapper object like so:
exportfunctionMyComponent() {const { classes } =useStyles();return <h1className={classes.root}>Hello World</h1>;}constuseStyles=makeStyles({ "name": { MyComponent } })({"root": {/*...*/ }});//...const { classes } =useStyles();//classes.root will be a string like: "tss-xxxxxx-MyComponent-root"
This prevent you from having to remember to update the label when you rename the component.
Beside the classes, useStyles also returns cx, css and your theme. css is the function as defined in @emotion/csscx is the function as defined in @emotion/css
In some components you may need cx, css or theme without defining custom classes.
For that purpose you can use the useStyles hook returned by createMakeStyles.
makeStyles.ts
import { createMakeAndWithStyles } from"tss-react";functionuseTheme() {return {"primaryColor":"#32CD32", };}exportconst {makeStyles,useStyles//<- This useStyles is like the useStyles you get when you// call makeStyles but it doesn't return a classes object.} =createMakeAndWithStyles({ useTheme });
./MyComponent.tsx
//Here we ca import useStyles directly instead of generating it from makeStyles.import { useStyles } from"./makeStyles";exportfunctionMyComponent(props:Props) {const { className } = props;const { cx,css,theme } =useStyles();return ( <spanclassName={cx(css({ "color":theme.primaryColor }), className)}> hello world </span> );}