passing className from the function in React

someone

I have an array of items and I want to change the background of some of the items.

Checking via the bool you are gonna see that part in switch.
className={classes.Items} this is working but how can I pass className here?

const createClassName = (data) => {
    const className = "Items";
    switch (data.isRead){
        case false:
            return className + "Unread";
        case true:
            return className ;
    }
};

{props.data.map((item) => (
    <MenuItem
    // className={classes.Items}
    //className={createClassNameForNotificationNeededRow(item)}
    key={item.key}
>
Akam

Looks like you use module style files to load classes and then you want to to conditionally apply them, in this case your data is a boolean, either true or false, and remember that classes you read from the imported style modules will be changed by some plugins before they make their way to browser to make it a unique className like items_knc5d5sx so keep in mind to use classes.someClassName instead using a string, anyway, here is a small suggestion for your use case:

const createClassName = (classes, data) => {
        return `${classes.items} ${!data.isRead ? classes.Unread : ""}`
};

Then use this function createClassName when you render to create the classes for each item dynamically.

{props.data.map((item) => (     
    <MenuItem
      className={createClassName(classes, item)}
      key={item.key}
    >
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

React: how return a string from arrow function and use it inside a ClassName

From Javascript

Passing data from one function to another in React

From Dev

Passing function from object to React Native ListView

From Dev

Passing info from a Form into a function React js

From Dev

Passing a value from one function to another function in React.js

From Dev

Passing function in React.js from one function component to another

From Dev

Passing {ClassName} vs passing {<ClassName>} as react component porps. What is the difference?

From Dev

Passing Function Down [React]

From Dev

Passing props to a function in react

From Dev

React passing function as prop

From Dev

passing a function to map in react

From Dev

passing data from child to parent component - react - via callback function

From Dev

Passing values from one function to another with React navigation 5

From Dev

Passing string parameters into function from one component to another in react

From Dev

Passing state up from child component and call a function with React

From Dev

React.js : Passing Props from a Function to a Component

From Dev

Passing state array from class into function with pure react native

From Dev

Passing function from mapDispatchToProps to Container component in react/redux app

From Dev

Proper way of passing function from one component to other on React typescript

From Dev

React typescript passing function as prop and calling from child component

From Dev

Passing Function Props in React-Typescript from parent to child

From Dev

Passing function via props from Parent to Child component in React?

From Dev

Passing className to a React/Tailwind component is not overriding the default classes

From Dev

call function inside of className react.js

From Dev

React/Jasvascript:Dynamic classname setting in map function

From Dev

How to put function in React className field?

From Dev

Passing Variables To/From Function

From Dev

Passing from a "lambda" function

From Dev

Passing down a className to a child component when called from another

Related Related

  1. 1

    React: how return a string from arrow function and use it inside a ClassName

  2. 2

    Passing data from one function to another in React

  3. 3

    Passing function from object to React Native ListView

  4. 4

    Passing info from a Form into a function React js

  5. 5

    Passing a value from one function to another function in React.js

  6. 6

    Passing function in React.js from one function component to another

  7. 7

    Passing {ClassName} vs passing {<ClassName>} as react component porps. What is the difference?

  8. 8

    Passing Function Down [React]

  9. 9

    Passing props to a function in react

  10. 10

    React passing function as prop

  11. 11

    passing a function to map in react

  12. 12

    passing data from child to parent component - react - via callback function

  13. 13

    Passing values from one function to another with React navigation 5

  14. 14

    Passing string parameters into function from one component to another in react

  15. 15

    Passing state up from child component and call a function with React

  16. 16

    React.js : Passing Props from a Function to a Component

  17. 17

    Passing state array from class into function with pure react native

  18. 18

    Passing function from mapDispatchToProps to Container component in react/redux app

  19. 19

    Proper way of passing function from one component to other on React typescript

  20. 20

    React typescript passing function as prop and calling from child component

  21. 21

    Passing Function Props in React-Typescript from parent to child

  22. 22

    Passing function via props from Parent to Child component in React?

  23. 23

    Passing className to a React/Tailwind component is not overriding the default classes

  24. 24

    call function inside of className react.js

  25. 25

    React/Jasvascript:Dynamic classname setting in map function

  26. 26

    How to put function in React className field?

  27. 27

    Passing Variables To/From Function

  28. 28

    Passing from a "lambda" function

  29. 29

    Passing down a className to a child component when called from another

HotTag

Archive