DEV Community

Himanshupal0001
Himanshupal0001

Posted on

2 1

Ways to pass multiple classes in react!!!

By passing props

import React from 'react';
import { makeStyles } from '@material-ui/core';

const useStyles = makeStyles({
    firstStyle: {
        backgroundColor: props => props.backgroundColor,
    },
    secondStyle: {
        color: props => props.color,
    },
});

const MyComponent = ({children, ...props}) =>{
    const { firstStyle, secondStyle } = useStyles(props);
    return(
        <div className={`${firstStyle} ${secondStyle}`}>
            {children}
        </div>
    )
}

export default MyComponent;
Now you can use it like:

<MyComponent color="yellow" backgroundColor="purple">
    Well done
</MyComponent>
Enter fullscreen mode Exit fullscreen mode

OffcialDocumentation

By clsx

First install it:

npm install --save clsx
Then import it in your component file:

import clsx from  'clsx';
Then use the imported function in your component:

<div className={ clsx(classes.class1, classes.class2)}>
Enter fullscreen mode Exit fullscreen mode

OfficialDocumentation

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay