React 中的自定义属性
Custom attributes in React
"If you pass properties to native HTML elements that do not exist in the HTML specification, React will not render them." - React 文档。
https://facebook.github.io/react/docs/jsx-gotchas.html#custom-html-attributes
是否存在性能问题或为什么不在 React 中呈现自定义属性?
因为 React 设计为不将任何数据传递给 html 属性,仅将数据存储在组件的状态或道具中。
试试这个方法你可以在控制台看到点击事件的属性值
//...
alertMessage (cEvent){
console.log(cEvent.target.getAttribute('customEvent')); /*display attribute value */
}
//...
简单地在 render 方法中添加 customAttribute
render(){
return <div>
//..
<button customAttribute="My Custom Event Message" onClick={this.alertMessage.bind(this) } >Click Me</button>
</div>
}
//...
"If you pass properties to native HTML elements that do not exist in the HTML specification, React will not render them." - React 文档。 https://facebook.github.io/react/docs/jsx-gotchas.html#custom-html-attributes
是否存在性能问题或为什么不在 React 中呈现自定义属性?
因为 React 设计为不将任何数据传递给 html 属性,仅将数据存储在组件的状态或道具中。
试试这个方法你可以在控制台看到点击事件的属性值
//...
alertMessage (cEvent){
console.log(cEvent.target.getAttribute('customEvent')); /*display attribute value */
}
//...
简单地在 render 方法中添加 customAttribute
render(){
return <div>
//..
<button customAttribute="My Custom Event Message" onClick={this.alertMessage.bind(this) } >Click Me</button>
</div>
}
//...