How to add multiple classes to a ReactJS Component?

后端 未结 29 2088
执念已碎
执念已碎 2020-11-27 09:16

I am new to ReactJS and JSX and I am having a little problem with the code below.

I am trying to add multiple classes to the className attribute on eac

相关标签:
29条回答
  • 2020-11-27 09:27

    You can create an element with multiple class names like this, I tryed these both way, its working fine...

    If you importing any css then you can follow this way : Way 1:

    import React, { Component, PropTypes } from 'react';
    import csjs from 'csjs';
    import styles from './styles';
    import insertCss from 'insert-css';
    import classNames from 'classnames';
    insertCss(csjs.getCss(styles));
    export default class Foo extends Component {
      render() {
        return (
          <div className={[styles.class1, styles.class2].join(' ')}>
            { 'text' }
          </div>
        );
      }
    }
    

    way 2:

    import React, { Component, PropTypes } from 'react';
    import csjs from 'csjs';
    import styles from './styles';
    import insertCss from 'insert-css';
    import classNames from 'classnames';
    insertCss(csjs.getCss(styles));
    export default class Foo extends Component {
      render() {
        return (
          <div className={styles.class1 + ' ' + styles.class2}>
            { 'text' }
          </div>
        );
      }
    }
    

    **

    If you applying css as internal :

    const myStyle = {
      color: "#fff"
    };
    
    // React Element using Jsx
    const myReactElement = (
      <h1 style={myStyle} className="myClassName myClassName1">
        Hello World!
      </h1>
    );
    
    ReactDOM.render(myReactElement, document.getElementById("app"));
    .myClassName {
      background-color: #333;
      padding: 10px;
    }
    .myClassName1{
      border: 2px solid #000;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.4.0/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.0/umd/react-dom.production.min.js"></script>
    <div id="app">
      
    </div>

    0 讨论(0)
  • 2020-11-27 09:29

    I use classnames when there is a fair amount of logic required for deciding the classes to (not) use. An overly simple example:

    ...
        var liClasses = classNames({
          'main-class': true,
          'activeClass': self.state.focused === index
        });
    
        return (<li className={liClasses}>{data.name}</li>);
    ...
    

    That said, if you don't want to include a dependency then there are better answers below.

    0 讨论(0)
  • 2020-11-27 09:29

    This can be achieved with ES6 template literals:

    <input className={`class1 ${class2}`}>
    
    0 讨论(0)
  • 2020-11-27 09:29

    When I have many varying classes, I have found the following to be useful.

    The filter removes any of the null values and the join puts all the remaining values into a space separated string.

    const buttonClasses = [
        "Button", 
        disabled ? "disabled" : null,
        active ? "active" : null
    ].filter((class) => class).join(" ")
    
    <button className={buttonClasses} onClick={onClick} disabled={disabled ? disabled : false}>
    
    0 讨论(0)
  • 2020-11-27 09:30

    You could do the following:

    <li key={index} className={`${activeClass} ${data.class} main-class`}></li>
    

    A short and simple solution, hope this helps.

    0 讨论(0)
  • 2020-11-27 09:31

    I use ES6 template literals. For example:

    const error = this.state.valid ? '' : 'error'
    const classes = `form-control round-lg ${error}`
    

    And then just render it:

    <input className={classes} />
    

    One-liner version:

    <input className={`form-control round-lg ${this.state.valid ? '' : 'error'}`} />
    
    0 讨论(0)
提交回复
热议问题