[
{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"}
];
即一个组件实现一个功能。
图中,一个颜色是一种组件,按照嵌套关系,可以分为:
FilterableProductTable:
SearchBar:
ProductTable:
ProductCategoryRow
ProductRow
1. 值不是来自与props
该示例中,需要数据源的地方有:
1. 查询框的文本 // 会变化;state.filterText
通过分析state对应的值作用的范围,state应该位于所有作用区域的父组件;
才能共享state。
从功能分析,1,2的状态会影响列表展示;则其应该位于查询框和列表的父组件;
即组件FilterableProductTable中。
3的状态会影响列表分类ProductCategoryRow和列表项ProductRow,则应该放在他们的父组件;
即组件ProductTable中。
即通过触发子组件的事件监听,调用父组件传递过去的函数,从而改变父组件的state的值。
因为state的状态提升,而state又是私有状态,只能通过state所处的组件的函数进行修改。
/*
* @Author: LyraLee
* @Date: 2019-11-08 09:01:58
* @LastEditTime: 2019-11-20 17:32:14
* @Description: 分类数据处理
*/
'use strict'
const products = [
{category: "Sporting Goods", price: "$49.99", stocked: true, name: "Football"},
{category: "Electronics", price: "$399.99", stocked: false, name: "iPhone 5"},
{category: "Sporting Goods", price: "$9.99", stocked: true, name: "Baseball"},
{category: "Electronics", price: "$99.99", stocked: true, name: "iPod Touch"},
{category: "Sporting Goods", price: "$29.99", stocked: false, name: "Basketball"},
{category: "Electronics", price: "$199.99", stocked: true, name: "Nexus 7"}
];
function ProductRow(props) {
const product = props.product;
const { name } = product;
const renderName = !product.stocked ? {name} : name
return (
return (
name | price |
---|
{' '} Only show products in stock
ReactDOM.render(
手机扫一扫
移动阅读更方便
你可能感兴趣的文章