Passing in class names to react components

前端 未结 9 623
说谎
说谎 2020-12-23 02:35

I am trying to pass in a classname to a react component to change it\'s style and cannot seem to get working:

class Pill extends React.Component {

  render(         


        
相关标签:
9条回答
  • 2020-12-23 03:14

    With React's support for string interpolation, you could do the following:

    class Pill extends React.Component {
        render() {
           return (
              <button className={`pill ${this.props.styleName}`}>{this.props.children}</button>
           );
        }
    }
    

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

    As other have stated, use an interpreted expression with curly braces.

    But do not forget to set a default.
    Others has suggested using a OR statement to set a empty string if undefined.

    But it would be even better to declare your Props.

    Full example:

    import React, { Component } from 'react';
    import PropTypes from 'prop-types';
    
    class Pill extends Component {
    
      render() {
    
        return (
          <button className={`pill ${ this.props.className }`}>{this.props.children}</button>
        );
      }
    
    }
    
    Pill.propTypes = {
      className: PropTypes.string,
    };
    
    Pill.defaultProps = {
      className: '',
    };
    
    0 讨论(0)
  • 2020-12-23 03:33

    For anyone interested, I ran into this same issue when using css modules and react css modules.

    Most components have an associated css module style, and in this example my Button has its own css file, as does the Promo parent component. But I want to pass some additional styles to Button from Promo

    So the styleable Button looks like this:

    Button.js

    import React, { Component } from 'react'
    import CSSModules from 'react-css-modules'
    import styles from './Button.css'
    
    class Button extends Component {
    
      render() {
    
        let button = null,
            className = ''
    
        if(this.props.className !== undefined){
            className = this.props.className
        }
    
        button = (
          <button className={className} styleName='button'>
            {this.props.children}
          </button>
        )
    
        return (
            button
        );
      }
    };
    
    export default CSSModules(Button, styles, {allowMultiple: true} )
    

    In the above Button component the Button.css styles handle the common button styles. In this example just a .button class

    Then in my component where I want to use the Button, and I also want to modify things like the position of the button, I can set extra styles in Promo.css and pass through as the className prop. In this example again called .button class. I could have called it anything e.g. promoButton.

    Of course with css modules this class will be .Promo__button___2MVMD whereas the button one will be something like .Button__button___3972N

    Promo.js

    import React, { Component } from 'react';
    import CSSModules from 'react-css-modules';
    import styles from './Promo.css';
    
    import Button from './Button/Button'
    
    class Promo extends Component {
    
      render() {
    
        return (
            <div styleName='promo' >
              <h1>Testing the button</h1>
              <Button className={styles.button} >
                <span>Hello button</span>
              </Button>
            </div>
          </Block>
        );
      }
    };
    
    export default CSSModules(Promo, styles, {allowMultiple: true} );
    
    0 讨论(0)
提交回复
热议问题