【问题标题】:Custom attributes in ReactReact 中的自定义属性
【发布时间】:2016-09-22 17:20:33
【问题描述】:

“如果你将属性传递给 HTML 规范中不存在的原生 HTML 元素,React 将不会呈现它们。” - 反应文档。 https://facebook.github.io/react/docs/jsx-gotchas.html#custom-html-attributes

是否存在性能问题或为什么 React 中没有呈现自定义属性?

【问题讨论】:

    标签: facebook reactjs polymer web-component


    【解决方案1】:

    因为 React 设计为不向 html 属性传递任何数据,仅将数据存储在组件的 state 或 props 中。

    【讨论】:

      【解决方案2】:

      试试这个方法你可以在点击事件的控制台中看到属性值

      //...
         alertMessage (cEvent){
                 console.log(cEvent.target.getAttribute('customEvent')); /*display attribute value */
              }
      //...
      

      在渲染方法中简单地添加 customAttribute 作为您的愿望

      render(){
          return <div>
      //..
          <button customAttribute="My Custom Event Message" onClick={this.alertMessage.bind(this) } >Click Me</button>
                  </div>
              }
      //...
      

      【讨论】:

        猜你喜欢
        • 2017-09-03
        • 2019-07-19
        • 2019-01-24
        • 1970-01-01
        • 2021-05-02
        • 2019-05-23
        • 2021-08-05
        • 2018-10-21
        • 1970-01-01
        相关资源
        最近更新 更多