How to add multiple classes in Material UI using the classes props

后端 未结 11 1960
花落未央
花落未央 2020-12-23 15:39

Using the css-in-js method to add classes to a react component, how do I add multiple components?

Here is the classes variable:

const styles = theme          


        
相关标签:
11条回答
  • 2020-12-23 16:09

    I think this will solve your problem:

    const styles = theme => ({
     container: {
      display: 'flex',
      flexWrap: 'wrap'
    },
     spacious: {
      padding: 10
    },
    });
    

    and in react component:

    <div className={`${classes.container} ${classes.spacious}`}>
    
    0 讨论(0)
  • 2020-12-23 16:16

    If you want to assign multiple class names to your element, you can use arrays.

    So in your code above, if this.props.classes resolves to something like ['container', 'spacious'], i.e. if

    this.props.classes = ['container', 'spacious'];
    

    you can simply assign it to div as

    <div className = { this.props.classes.join(' ') }></div>
    

    and result will be

    <div class='container spacious'></div>
    
    0 讨论(0)
  • 2020-12-23 16:17

    To have multiple classes applied to a component, wrap the classes you would like to apply within classNames.

    For example, in your situation, your code should look like this,

    import classNames from 'classnames';
    
    const styles = theme => ({
      container: {
        display: "flex",
        flexWrap: "wrap"
      },
      spacious: {
        padding: 10
      }
    });
    
    <div className={classNames(classes.container, classes.spacious)} />
    

    Make sure that you import classNames!!!

    Have a look at material ui documentation where they use multiple classes in one component to create a customized button

    0 讨论(0)
  • 2020-12-23 16:17

    Yes, jss-composes provides you this:

    const styles = theme => ({
     container: {
      display: 'flex',
      flexWrap: 'wrap'
    },
     spacious: {
      composes: '$container',
      padding: 10
    },
    });
    

    And then you just use classes.spacious.

    0 讨论(0)
  • 2020-12-23 16:18

    You can also use the extend property (the jss-extend plugin is enabled by default):

    const styles = theme => ({
      container: {
        display: 'flex',
        flexWrap: 'wrap'
      },
      spaciousContainer: {
        extend: 'container',
        padding: 10
      },
    });
    
    // ...
    <div className={ this.props.classes.spaciousContainer }>
    
    0 讨论(0)
  • 2020-12-23 16:25

    you can install this package

    https://github.com/JedWatson/classnames

    and then use it like this

    classNames('foo', 'bar'); // => 'foo bar'
    classNames('foo', { bar: true }); // => 'foo bar'
    classNames({ 'foo-bar': true }); // => 'foo-bar'
    classNames({ 'foo-bar': false }); // => ''
    classNames({ foo: true }, { bar: true }); // => 'foo bar'
    classNames({ foo: true, bar: true }); // => 'foo bar'
    
    // lots of arguments of various types
    classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'
    
    // other falsy values are just ignored
    classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'
    
    0 讨论(0)
提交回复
热议问题