react 高阶组件

react

高阶函数对大家可能有点陌生,我们把高阶函数分开来看,先看什么是高阶然后再看什么是组件。

高阶函数

  • 可以有效降低复杂性和组件的体积
  • 便于阅读和理解
  • 减少bug 产生机会

在函数式编程中,就是有高阶函数概念,所谓高阶就是函数可以作为函数的参数和函数返回值

const arr = [1,2,3,4,5];
for (let i = 0; i < arr.length; i++) {
  const element = arr[i];
  console.log(element)
}

下面代码就是函数式编程,好处是没有产生中间变脸element 和 i,更容易阅读和理解,其实大家认为上面的代码更容易阅读和了解,之所以你这样认为那是因为你更熟悉这种方式 coding,并不能说明这样 coding 更便于阅读。


const arr1 = [1,2,3,4,5];
arr1.forEach(element=>console.log(element))

react 组件
组件实际上就是函数
使用组件可以提供高复用
易于维护

高阶组件

import React, { Component } from 'react';

 const RandomPosition = (WrappdComponent) =>{
    return class extends React.Component{
        constructor(props){
            super(props);
            this.state = {
                position:{
                    top:`${Math.floor(Math.random() * 100)}vh`,
                    left:`${Math.floor(Math.random() * 100)}vw`
                }
            };
        }

        render(){
            return <WrappdComponent {...this.props} position={this.state.position}/>
        }
    }
}

export default RandomPosition;

定义组件 RandomPosition 函数接收一个 WrappdComponent 组件作为参数,然后返回一个扩展(继承)了 Component 的匿名类(组件)做为返回值。典型的高阶组件呀。这个对传入组件进行定位。

import React, { Component } from 'react';
import RandomPosition from './RandomPosition';

const ReactLogo = (props) =>{
  return(
    <img
      style={props.position}
      onClick={props.jump}
      onMouseOver={props.flip}
      src="../public/react-logo.png"
    />
  )
}

const ExtendedReactLogo= RandomPosition(ReactLogo);

export default class Greeting extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            count:0
        }
        this.incrementCount = this.incrementCount.bind(this);
        this.resetCount = this.resetCount.bind(this);
    }

    incrementCount(){
        this.setState({
            count:this.state.count + 1
        });
    }

    resetCount(){
        this.setState({
            count:0
        })
    }

    render(){
        const children = [];
        for(let i  =0; i < this.state.count; i++){
            children.push(<ExtendedReactLogo key={i}/>);
        }

        return (
            <div className="container">
                {children}
                <br/>
                <button className="btn btn-blue count" 
                    onClick={this.incrementCount}
                    >
                    <h1>添加</h1>
                    </button>
            </div>
        )
    }
}
const ExtendedReactLogo= RandomPosition(ReactLogo);

上面的代码中定义 ReactLogo 这个图片组件,然后作为参数传入给 RandomPosition 生成 ExtendedReactLogo 组件。

javascript-illustration.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 学习react已经有一段时间了,期间在阅读官方文档的基础上也看了不少文章,但感觉对很多东西的理解还是不够深刻...
    Srtian阅读 5,591评论 0 7
  • 高阶组件是对既有组件进行包装,以增强既有组件的功能。其核心实现是一个无状态组件(函数),接收另一个组件作为参数,然...
    柏丘君阅读 8,260评论 0 6
  • 1. Decorator基本知识 在很多框架和库中看到它的身影,尤其是React和Redux,还有mobx中,那什...
    cbw100阅读 4,643评论 1 7
  • React进阶之高阶组件 前言 本文代码浅显易懂,思想深入实用。此属于react进阶用法,如果你还不了解react...
    流动码文阅读 4,852评论 0 1
  • 第一章 引导 学习之前要有一定的:es6基础、js面向对象编程、react的一些基础知识 第二章 高阶组件介绍 2...
    读书的鱼阅读 5,734评论 0 5