Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 9, 2022 10:37 am GMT

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>

OffcialDocumentation

By clsx

First install it:npm install --save clsxThen 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)}>

OfficialDocumentation


Original Link: https://dev.to/himanshupal0001/ways-to-pass-multiple-classes-in-react-p46

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To