我的网站开发技术经验总结 我的网站开发技术经验总结
首页

fangdown

我的网站开发技术经验总结
首页
  • 大前端

  • 基础

  • 框架

    • react

      • 说说对React的理解,有哪些特性
      • 说说对React高阶组件的理解
        • 是什么
        • 怎么用
          • 示例
          • 作用
          • 应用
        • 原理
        • FAQ
      • 谈谈React中fiber的理解
      • 说说对React事件机制的理解
      • 说说对React中受控组件和非受控组件的理解及应用场景
      • 说说对redux工作流程的理解
      • 说说对redux中间件的理解
      • 说说对state和props的理解,有什么区别
      • 说说函数组件和类组件的理解和区别
      • 说说真实DOM和VDOM的区别,优缺点
      • 说说React.memo&useMemo&useCallback区别
      • 说说React路由有几种模式以及实现原理
      • 说说React生命周期有哪些不同阶段?每个阶段对应的方法
      • 说说React事件绑定有哪些
      • 说说React中的setState执行机制
      • 说说React中refs的理解
      • React构建组件有哪些方式及区别
      • React引入css的方式有哪几种
      • 谈谈React中key的作用
      • React中如何性能优化
      • React中hooks能取代redux吗
      • React组件通信方式有哪些
  • 情商

  • 算法

  • 网络

  • 千锤百炼
  • 框架
  • react
fangdown
2021-08-01
目录

说说对React高阶组件的理解

# 是什么

接受一个或多个组件为参数并且返回一个组件

# 怎么用

# 示例

import React, { Component } from 'react';

export default (WrappedComponent) => {
  return class EnhancedComponent extends Component {
    // do something
    render() {
      return <WrappedComponent />;
    }
  }
}

# 作用

  • 代码复用,逻辑抽象,抽离底层代码
  • 渲染劫持
  • 追加props属性

# 应用

  • 读取缓存
import React, { Component } from 'react'

function withPersistentData(WrappedComponent) {
  return class extends Component {
    componentWillMount() {
      let data = localStorage.getItem('data');
        this.setState({data});
    }
    
    render() {
      // 通过{...this.props} 把传递给当前组件的属性继续传递给被包装的组件WrappedComponent
      return <WrappedComponent data={this.state.data} {...this.props} />
    }
  }
}

class MyComponent2 extends Component {  
  render() {
    return <div>{this.props.data}</div>
  }
}

const MyComponentWithPersistentData = withPersistentData(MyComponent2)
  • 性能监控
class Home extends React.Component {
    render() {
        return (<h1>Hello World.</h1>);
    }
}
function withTiming(WrappedComponent) {
    return class extends WrappedComponent {
        constructor(props) {
            super(props);
            this.start = 0;
            this.end = 0;
        }
        componentWillMount() {
            super.componentWillMount && super.componentWillMount();
            this.start = Date.now();
        }
        componentDidMount() {
            super.componentDidMount && super.componentDidMount();
            this.end = Date.now();
            console.log(`${WrappedComponent.name} 组件渲染时间为 ${this.end - this.start} ms`);
        }
        render() {
            return super.render();
        }
    };
}

export default withTiming(Home);

# 原理

# FAQ

#react
上次更新: 2021/12/19, 18:05:42
说说对React的理解,有哪些特性
谈谈React中fiber的理解

← 说说对React的理解,有哪些特性 谈谈React中fiber的理解→

最近更新
01
多分支修复撞车的问题
05-01
02
如何成为架构师
01-23
03
服务器部署全过程
11-23
更多文章>
Theme by Vdoing | Copyright © 2019-2026 fangdown | 粤ICP备19079809号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式