react 哲学_React哲学案例练习

1.Object.assign()用法讲解:
https://blog.csdn.net/Calla_Lj/article/details/894787652.React Context(上下文) 作用和使用:
https://www.jianshu.com/p/65b348bf86ad3.浅谈react context:
https://segmentfault.com/a/11900000202179264.react Portals:
https://reactjs.bootcss.com/docs/portals.html

toString()不常用用法:

//使用radix进制把number数字转换为字符串
number.toString(radix);

React哲学案例练习:

8524ae75c860b2b3269895ad5c8f1745.png

b250a7148483886a1785a650177df777.png

import React from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';

class ProductCategoryRow extends React.Component{
    constructor(props){
        super(props);
    }

    render(){
        var category = this.props.category;
        return (
            '2'>{category}
        )
    }
}

        class ProductRow extends React.Component{
            constructor(props){
                super(props);
            }

            render(){
                var productName = this.props.productName;
                var productPrice = this.props.productPrice;
                return (
                    {productName}{productPrice}
                )
            }
        }

        class ProductTable extends React.Component{
            constructor(props){
                super(props);
            }

            render(){
                var filter = this.props.filterText;
                var inStockOnly = this.props.inStockOnly;
                var stockData = this.props.productData;
                if(!stockData){
                    return Loading...
                } else {
                    let sportList = [];
                    let elecList = [];
                    var conditional = '';
                    if (filter === ''){
                        conditional = true;
                    }
                    stockData.map((item)=>{
                        if(conditional){
                            if(item.category === 'Sporting Goods'){
                                sportList.push(item);
                            } else {
                                elecList.push(item);
                            }
                        } else if((item.name.indexOf(filter) !== -1) && (item.stocked === inStockOnly)){
                            if(item.category === 'Sporting Goods'){
                                sportList.push(item);
                            } else {
                                elecList.push(item);
                            }
                        }
                    })

                    const sports = sportList.map((element,index)=>
                        
                    )

                    const elects = elecList.map((element,index)=>
                        
                    )

                    return(
                        'Sporting Goods'/>
                            {sports}'Electronics'/>
                            {elects}
                    )
                }
            }
        }

        class SearchBar extends React.Component{
            constructor(props){
                super(props);
                this.handleEnterKey = this.handleEnterKey.bind(this);
                this.handleClick = this.handleClick.bind(this);
            }

            handleEnterKey = (e) =>{
                if(e.nativeEvent.keyCode === 13){   //e.nativeEvent获取原生的事件对象
                    var filterText = this.search.value;
                    this.props.filter(filterText);
                }
            }

            handleClick(){
                var inStock = this.checkbox.checked;
                this.props.check(inStock);
            }

            render(){
                return (
                    
'text' placeholder='Search...' onKeyPress={this.handleEnterKey} ref={ref => this.search = ref}/>'checkbox' id='stockCheck' onClick={this.handleClick} ref={ref => this.checkbox = ref}/>'stockCheck'>Only show productPrices in stock

                )
            }
        }

        class FilterableProductTable extends React.Component{
            constructor(props){
                super(props);
                this.state = {stockData:'',filterText:'',inStockOnly:false};
                this.getFilter = this.getFilter.bind(this);
                this.getChecked = this.getChecked.bind(this);
            }

            componentDidMount(){
                const _this = this;      //先存一下this,以防止使用箭头函数this会指向不希望它所指向的对象
                axios.get('http://localhost:3000/api/stockProduct.json').then((res) => {
                    const refreshData = res.data;
                    _this.setState({
                        stockData: refreshData
                    })
                }).catch(function(error){
                    console.log(error);
                    _this.setState({stockData: []})
                });
            }

            getFilter(filter){
                this.setState({
                    filterText: filter 
                })
            }

            getChecked(inStock){
                this.setState({
                    inStockOnly: inStock 
                })
            }

            render(){
                return (
                    
'searchBar' filter={this.getFilter} check={this.getChecked}/> NamePricethis.state.stockData} filterText={this.state.filterText} inStockOnly={this.state.inStockOnly}/>

                )
            }
        }

        ReactDOM.render(
            ,
            document.getElementById('root')
        )
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值