TSS
HomeGitHubPlayground
v3
v3
  • 🚀Why TSS
  • 🔧Setup
  • 🔍API References
    • makeStyles -> useStyles
    • withStyles
    • <GlobalStyles />
    • keyframes
    • useMergedClasses
  • 💽Cache
  • 💫Nested selectors (ex $ syntax)
  • ⚡SSR
    • Gatsby
    • Next.js
    • Other backends
  • 🦱Your own classes prop
  • 🍭MUI Theme styleOverrides
  • 🧹Detecting unused classes
  • 📦Publish a module that uses TSS
  • 🔩single-spa
  • 📲React Native
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. API References

useMergedClasses

PreviouskeyframesNextCache

Last updated 2 years ago

Was this helpful?

This API is not deprecated but the new recommended way is to do:

-let { classes } = useStyles();
-classes = useMergedClasses(classes, props.classes);
+const { classes } = usesStyles(undefined, { props });

This would also work (mentioned just so you understand how it works):

const { classes } = usesStyles(
    undefined, 
    { "props": { "classes": props.classes } }
);

Merge the internal classes and the one that might have been provided as props into a single classes object.

This is the new way for . See .

import { useMergedClasses } from "tss-react";

type Props = {
    //classes?: { foo?: string; bar?: string; };
    classes?: Partial<ReturnType<typeof useStyles>["classes"]>;
};

function MyTestComponentForMergedClassesInternal(props: Props) {
    let { classes } = useStyles();
    classes = useMergedClasses(classes, props.classes);
    


    return (
        <div className={classes.foo}>
            <span className={classes.bar}>
                The background should be green, the box should have a dotted
                border and the text should be pink
            </span>
        </div>
    );
}

const useStyles = makeStyles()({
    "foo": {
        "border": "3px dotted black",
        "backgroundColor": "red"
    }
    "bar": {
        "color": "pink"
    }
});

//...

render(
    <MyTestComponentForMergedClassesInternal
        classes={{ "foo": css({ "backgroundColor": "green" }) }}
    />
);

NOTE: You may end up with eslint warnings if you deconstruct more that one item. Don't hesitate to disable eslint(prefer-const): in a regular project, or in a CRA.

🔍
Overriding styles - classes prop
this issue
Result
like this one
Like this
like this