javascript - React渲染出的结果与期望结果不同?
迷茫
迷茫 2017-04-11 09:55:38
[JavaScript讨论组]

Chrome中的React dev tool显示dom结构是这样的(也符合我的代码):

可chrome中实际显示出来的效果却是这样的:

可以看到,ProductCategoryRow跑到了SearchBarProductTable的中间(图中圈出来的部分)。

附上代码:

var data = [
  {category: "Sporting Goods", price: "$49.99", stocked: true, name: "Football"},
  {category: "Sporting Goods", price: "$9.99", stocked: true, name: "Baseball"},
  {category: "Sporting Goods", price: "$29.99", stocked: false, name: "Basketball"},
  {category: "Electronics", price: "$99.99", stocked: true, name: "iPod Touch"},
  {category: "Electronics", price: "$399.99", stocked: false, name: "iPhone 5"},
  {category: "Electronics", price: "$199.99", stocked: true, name: "Nexus 7"}
];
        var ProductRow = React.createClass({
          render(){
            return <tr>
                <td>{this.props.name}</td>
                <td>{this.props.price}</td>
              </tr>
          },
        });
        var ProductCategoryRow = React.createClass({
          render(){
            return <tr>
                {this.props.category}
              </tr>
          },
        });
        var ProductTable = React.createClass({
          render(){
            var category = [];
            this.props.data.map(function(item){
              if(!category.includes(item.category)){
                category.push(item.category);
              }
            });
            var row = [];
            for (let value of category){
              row.push(<ProductCategoryRow category={value} key={value} />);
              this.props.data.map(function(item){
                if(item.category == value){
                  row.push(<ProductRow name={item.name} price={item.price} key={item.name}/>);
                }
              })
            } 
            return <table>            
              <tbody>
               <tr>
                <th>Name</th><th>Price</th>  
              </tr>    
              {row}
              </tbody>
              </table>
          }
        });
        var SearchBar = React.createClass({
          render(){
            return(<form>
              <input type="text"placeholder="search"/>
              <input type="checkbox"/>
              </form>)
          }
        });
        var FilterableProductTable = React.createClass({
          render(){
            return <p>
              <SearchBar />
              <ProductTable data={this.props.data}/>
              </p>
          }
        })
        ReactDOM.render(<FilterableProductTable data={data}/>
          ,document.getElementById('content'));
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(1)
怪我咯

tr 里面要有 tdth,你直接在 tr 里面放文字浏览器肯定不能正常渲染

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号