传递类名以反应组件

hil*_*arl 75 javascript reactjs

我试图将一个类名传递给一个react组件来改变它的样式,似乎无法正常工作:

class Pill extends React.Component {

  render() {

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

}

<Pill styleName="skill">Business</Pill>
Run Code Online (Sandbox Code Playgroud)

我试图通过传递具有相应风格的类的名称来改变药丸的风格.我是React的新手,所以也许我没有以正确的方式做到这一点.谢谢

gce*_*edo 114

在React中,当您想要传递解释的表达式时,您必须打开一对花括号.尝试:

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

使用类名 npm包

import classnames from 'classnames';

render() {
  return (
    <button className={classnames('pill', this.props.styleName)}>
      {this.props.children}
    </button>
  );
}
Run Code Online (Sandbox Code Playgroud)

  • 为什么它更好:性能?可读性?其他 ?文字字符串(您的第一个示例)是ES6的一部分,因此它是标准.它减少了代码并避免导入.它*对我来说感觉更好,但另一种解决方案可能有争议. (3认同)
  • 需要注意的重要一点是:如果“props.styleName”是可选的,并且您省略了它,那么您的“class”列表中将有“undefined”。所以 ``{`pill ${ this.props.styleName }`}`` 不太好。此外,末尾还会有多余的空格。 (3认同)
  • 在上面的例子中,你是绝对正确的,最好使用ES6字符串.我会说当你必须处理条件时,类名在可读性和DRY方面更好,比如在类名中自述文件显示https://github.com/JedWatson/classnames#usage-with-reactjs. (2认同)

Mah*_*hdi 13

仅供参考,对于无状态组件:

// 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 = ( props ) =>
  <div className={ `some-css-className ${ props.className }` }>
    { props.children }
  </div>
Run Code Online (Sandbox Code Playgroud)

将呈现:

<div class="parent-component">
  <div class="some-css-className parent-component__child">
    ...
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • @theSereneRebel 不,事实并非如此。请参阅此处的示例:https://codesandbox.io/s/clever-knuth-enyju (2认同)
  • @theSereneRebel 这是否是一件好事是另一个话题。 (2认同)

小智 11

pill ${this.props.styleName} 当你没有设置道具时,会得到"未定义药丸"

我更喜欢

className={ "pill " + ( this.props.styleName || "") }
Run Code Online (Sandbox Code Playgroud)

要么

className={ "pill " + ( this.props.styleName ? this.props.styleName : "") }
Run Code Online (Sandbox Code Playgroud)


svn*_*vnm 7

对于任何感兴趣的人,我在使用css模块反应css模块时遇到了同样的问题.

大多数组件都有一个关联的css模块样式,在这个例子中,我的Button有自己的css文件,Promo父组件也是如此.但我想一些额外的风格传递给巴顿促销

所以功能style强大的Button看起来像这样:

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} )
Run Code Online (Sandbox Code Playgroud)

在上面的Button组件中,Button.css样式处理常见的按钮样式.在这个例子中只是一个.button

然后在我想要使用Button的组件中,我也想修改按钮位置之类的东西,我可以设置额外的样式Promo.css并作为className道具传递.在这个例子中又称为.buttonclass.我可以把它称为任何东西,例如promoButton.

当然,对于css模块,这个类将是,.Promo__button___2MVMD而按钮一个将是类似的.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} );
Run Code Online (Sandbox Code Playgroud)


Rus*_*ust 7

在 Typescript 中,您需要设置HTMLAttributes和的类型React.FunctionComponent

在大多数情况下,您需要将其扩展到另一个接口或类型。

const List: React.FunctionComponent<ListProps &
  React.HTMLAttributes<HTMLDivElement>> = (props) => {
  return (
    <div className={props.className}>
      <img className="mr-3" src={props.icon} alt="" />
      {props.context}
    </div>
  );
};

interface ListProps {
  context: string;
  icon: string;
}
Run Code Online (Sandbox Code Playgroud)


jam*_*lin 6

正如其他人所说,使用带花括号的解释表达式.

但是别忘了设置默认值.
其他人建议使用OR语句设置空字符串if undefined.

但是宣布你的道具会更好.

完整示例:

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: '',
};
Run Code Online (Sandbox Code Playgroud)