如何在JSX节点上指定类?

Hou*_*man 17 javascript reactjs react-jsx

我为下表定义了一个类名(作为JSX的一部分).
<table class="table">

但是,一旦显示该类未在表上设置:

var SearchResult = React.createClass({
       render: function(){
           return (
               <table class="table">
                   <thead>
                       <tr>
                           <th>Name</th>
                           <th>Address</th>
                       </tr>
                   </thead>
                   <tbody>...</tbody>
               </table>
           );
       }
    });
Run Code Online (Sandbox Code Playgroud)

相反,表格显示<table data-reactid=".0.1.0.0">...</table>在Chrome中 - > inspect元素.

cou*_*and 25

ReactJS使用该属性className来避免使用JavaScript保留字.

<table className="table">
Run Code Online (Sandbox Code Playgroud)

  • 正如[提问者在此提及](https://www.quora.com/Why-do-I-have-to-use-className-instead-of-class-in-ReactJs-components-done-in-JSX) ,JSX是经过预处理的,因此保留字并不是真正的动力.来自"Ben Alpert,React核心团队":"*我们当然可以用相反的方式完成它.我争论了一段时间......首先,我们倾向于查看HTML属性(如`el.className =. ..`)尽可能,而不是属性(如`el.setAttribute('class',...)`).*"等等. (3认同)