Passing in class names to react components

前端 未结 9 622
说谎
说谎 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:06

    In React, when you want to pass an interpreted expression, you have to open a pair of curly braces. Try:

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

    Using the classnames npm package

    import classnames from 'classnames';
    
    render() {
      return (
        <button className={classnames('pill', this.props.styleName)}>
          {this.props.children}
        </button>
      );
    }
    
    0 讨论(0)
  • 2020-12-23 03:07

    With React 16.6.3 and @Material UI 3.5.1, I am using arrays in className like className={[classes.tableCell, classes.capitalize]}

    Try something like the following in your case.

    class Pill extends React.Component {
        render() {
            return (
               <button className={['pill', this.props.styleName]}>{this.props.children}</button>
            );
        }
    }
    
    0 讨论(0)
  • 2020-12-23 03:08

    You can achieve this by "interpolating" the className passed from the parent component to the child component using this.props.className. Example below:

    export default class ParentComponent extends React.Component {
      render(){
        return <ChildComponent className="your-modifier-class" />
      }
    }
    
    export default class ChildComponent extends React.Component {
      render(){
        return <div className={"original-class " + this.props.className}></div>
      }
    }
    
    0 讨论(0)
  • 2020-12-23 03:11

    In Typescript you need to set types of HTMLAttributes and React.FunctionComponent.

    In most cases you will need will be extending it to another interface or type.

    const List: React.FunctionComponent<ListProps &
      React.HTMLAttributes<HTMLDivElement>> = (
      props: ListProps & React.HTMLAttributes<HTMLDivElement>
    ) => {
      return (
        <div className={props.className}>
          <img className="mr-3" src={props.icon} alt="" />
          {props.context}
        </div>
      );
    };
    
    interface ListProps {
      context: string;
      icon: string;
    }
    
    0 讨论(0)
  • 2020-12-23 03:12

    Just for the reference, for stateless components:

    // ParentComponent.js
    import React from 'react';
    import { ChildComponent } from '../child/ChildComponent';
    
    export const ParentComponent = () =>
      <div className="parent-component">
        <ChildComponent className="parent-component__child">
          ...
        </ChildComponent>
      </div>
    
    // ChildComponent.js
    import React from 'react';
    
    export const ChildComponent = ({ className, children }) =>
      <div className={`some-css-className ${className}`}>
        {children}
      </div>
    

    Will render:

    <div class="parent-component">
      <div class="some-css-className parent-component__child">
        ...
      </div>
    </div>
    
    0 讨论(0)
  • 2020-12-23 03:12

    pill ${this.props.styleName} will get "pill undefined" when you don't set the props

    I prefer

    className={ "pill " + ( this.props.styleName || "") }
    

    or

    className={ "pill " + ( this.props.styleName ? this.props.styleName : "") }
    
    0 讨论(0)
提交回复
热议问题