React学习-业务中模块的拆分

前端之家收集整理的这篇文章主要介绍了React学习-业务中模块的拆分前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

[React学习] 业务中模块的拆分

根据官网的例子 React编程思想
props 是一种从父级传递数据到子级的方式。

Example

父级:

  1. var FilterableProductTable = React.createClass({
  2. render: function@H_404_20@(){
  3. return (
  4. <div> <SearchBar /> <ProductTable products={this.props.products} /> </div> ) } });

子级:

  1. var ProductTable = React.createClass({
  2. render: function@H_404_20@(){
  3. var rows = [];
  4. var lastCategory = null;
  5. this.props.products.forEach(function@H_404_20@(product){ //通过this.props.products 来获取父级传过来的数据
  6. if(product.category !== lastCategory){
  7. rows.push(
  8. // 插入分类标题的头部
  9. <ProductCategoryRow category={product.category} key={product.category} />
  10. )
  11. }
  12. rows.push(
  13. <ProductRow product={product} key={product.name} /> ); lastCategory = product.category; }); return ( <table className="productTable"> <thead> <tr> <th>Name</th> <th>Price</th> </tr> </thead> <tbody>{rows}</tbody> </table> ) } });

根据官网示例写的代码(codepen)

收获

在根据UI拆分模块的时候,根据数据模型进行拆分。
代码编写的时候,在较简单的例子里,通常自顶向下要容易一些,然而在更大的项目上,自底向上地构建更容易

-FilterableProductTable (整体) -SearchBar(输入搜索框) -ProductTable(显示的数据表格) -ProductCategoryRow(分类名/列表头) -ProductRow(每一行的商品)

猜你在找的React相关文章