【问题标题】:How to automatically add appropriate class name to the React elements如何自动将适当的类名添加到 React 元素
【发布时间】:2016-08-20 00:41:29
【问题描述】:

我的目标是将类名添加到生成的 html 元素中。类名必须与反应组件类相对应。出于测试目的,这是必要的。 IE。当我声明<SomeClassComponent/> 或使用React.createElement(SomeClassComponent) 创建元素时,我希望在输出中有<div class="SomeClassComponent">。 如何在不更改 React 源代码的情况下实现这一点?

【问题讨论】:

  • 我不确定您打算如何在不更改任何代码的情况下执行此操作...
  • 你是对的。我的意思是,不改变 React 的源代码。例如使用继承和覆盖。
  • 哦...是的,这几乎是给定的。很高兴您更新了问题。下班后我会调查的。

标签: javascript html reactjs


【解决方案1】:

this.constructor.name 应该是这样做的方式。 JSFiddle

<div className={this.constructor.name}>

【讨论】:

  • 没关系,但我需要自动命名类而不重写现有代码。
猜你喜欢
  • 2019-06-24
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 2021-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多