Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle multiple className in makeStyle() - ReactJS

Tags:

reactjs

I'm trying to handle multiple className(s) to customize the style of the HTML element, but i don't understand how I can replicate the situation .class1.class2 (css) with the makeStyle method of React.

The situation is pretty simple, I've some blocks with className slide and one of them will always be active (so className=({classes.slide: true}, {classes.active: true})

In css I would've defined in such a way:

.slide {
  <some styles>
}

.slide.active {
  <some other styles>
}

Can i do something similar with makeStyle() or should I replace {classes.active: true} with {classes.activeSlide: true} and handle the situation like the following:

slide: {
  <some styles>
},

activeSlide: {
  <some other slide>
},
like image 945
Samuele Marchisio Avatar asked Nov 16 '19 09:11

Samuele Marchisio


People also ask

How do I pass multiple Classnames in React?

Adding a single class name import React from 'react'; import './styles. css'; function App(){ return ( <div className="container"> <h1>Hello rock!! </h1> </div> ) } export default App; If you want to add multiple class names, you can add it to the className attribute by separating with spaces.

How do I use multiple Classnames in material UI?

To add multiple classes in React Material UI using the classes props, we can use the classnames package. We call makeStyles with an object with the class names as the property names. And we set each property to an object with the CSS styles as the value. Next, we call useStyles to return the classes object.

Can tags have multiple classes?

HTML elements can be assigned multiple classes by listing the classes in the class attribute, with a blank space to separate them.

How do I use Classnames in React?

In class-based components, the className attribute is used to set or return the value of an element's class attribute. Using this property, the user can change the class of an element to the desired class. Creating React Application And Installing Module: Step 1: Create a React application using the following command.


2 Answers

:active is a pseudo class. makestyles provides the solution for all psuedo classes as follows:

const useStyles = makeStyles(theme => ({
 slide: {
  color: '#000',

  '&:active': {
   color: '#fff'
  }
 }
)})

Also, when using makeStyles you have to call const classes = useStyles() to get access to use the class names like classes.slide as the value of the className attribute in any jsx/html element in the render method.

EDIT/UPDATE:

In material ui makeStyles method, You want to write a common style rule for multiple classes when an element has both the classes attributed to it. So, for material ui makeStyles method, you can define multiple classes inside makeStyles like:

const useStyles = makeStyles(theme => ({
 class1: {
  marginTop: 100,
 },
 class2: {}
}))

Also attach them to the element using template literal like:

<div className={`${classes.class1} ${classes.class2}`}>

Now, to make class1 and class2 work as a combination we can do this:

const useStyles = makeStyles(theme => ({
 class1: {
  marginTop: 100,

  '&$class2': {
   color: 'red'
  }

 },
 class2: {}
}))

so even if class2 is not required in isolation, we need to define a dummy object so that we can have access to it when required inside render using classes.class2 and if required you can add styles to it which will work as any other className.

Also, found a documentation reference to this here .

like image 156
Subham Dey Avatar answered Nov 09 '22 05:11

Subham Dey


You can access your current classname inside its value object using the '&' character. Here is how it would look like in your case:

const classes = {
  slide: {
    backgroundColor: 'teal',

    '&.active': {
       backgroundColor: 'gold'
     }

  }
}
like image 42
giuseppedeponte Avatar answered Nov 09 '22 06:11

giuseppedeponte