1

How do I use a CSS module class in a function? an example: I have the className {styles.item} but I want to use that within a function such as...

export default function MenuItem {
if(props.type == "extra"){
   {styles.item}.classList.add("extra");
}
return (
    <MenuItem name="test product" type="extra" />
);
}

I have tried using it just as styles, with {} and with ${}

So what I want to do is if the has a type of "extra" then I want to add a dashed border.

0

4 Answers 4

1

https://github.com/JedWatson/classnames

You can install this page and use it in your react app

npm install classnames or yarn add classnames.

The Css

.success {
  color: green;
}
.error {
  color: red;
}

The JS

import styles from './alert.module.css'
import cn from 'classnames'

export default function Alert({ children, type }) {
  return (
    <div
      className={cn({
        [styles.success]: type === 'success',
        [styles.error]: type === 'error'
      })}
    >
      {children}
    </div>
  )
}
Sign up to request clarification or add additional context in comments.

Comments

0
   <div className={`anyClass ${props.type==='extra' ? 'extra' : null}`}/>

3 Comments

Then how do I use this in a function? I have updated the main post to contain a little more code
use this in the component that requires this conditional class list. this won't work in a function
A little explanation here (or, at least, a brief commentary) would improve this answer. (I came across it in the Low Quality Posts review queue.)
0

styles.item is a string (a class name).

It's not clear what you're trying to do with {styles.item}.classList.add( ... ).

If you want to further distinguish "extra" from "item" you should add an additional class to your stylesheet and pass them both:

<MenuItem className={`${styles.item} ${styles.extra}`} />

There are libraries like clsx to help with conditionally assembling class names:

const classes = clsx(
  styles.item,
  {
    [styles.extra]: props.type === 'extra'
  }
);

<MenuItem className={classes} />

You can use it in a function just like any other string or variable:

function Foo () {
  // nothing special about styles.item. it's just a string.
  const classes = props.type === 'extra'
    ? `${styles.item} ${styles.extra}`
    : styles.item;

  return classes;
}

2 Comments

Hey ray I tried implementing this with an onclick function essentially changing the class on a click. Although in the dom it is reflecting the change, the css is not being applied. The class in the html was updated, but the css style that corresponds to the added class was not applied.
Can't say without seeing it, but it sounds like either 1) the css selector doesn't apply to the element you're trying to target, or 2) the existing rules you're trying to override have a higher specificity so they take precedence over your new rules.
0

Say you had the following function:

const getStyleClass = () => 
   if (condition){
      return "one"
   } else {
      return "two
   } 

You could use the function like so:

    <MenuItem name="test product" type="extra"  className = {() => {getStyleClass()}}/>

Keep in mind however, that the function will be called once and updating the css class dynamically will require a bit more work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.