Passing in class names to react components

前端 未结 9 723
说谎
说谎 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: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 (
          
        );
      }
    
    }
    
    Pill.propTypes = {
      className: PropTypes.string,
    };
    
    Pill.defaultProps = {
      className: '',
    };
    

提交回复
热议问题