修饰器&高阶组件

一、修饰器

1、类的修饰

修饰器是一个函数,用来修改类的行为

function testable(target) {
  target.isTestable = true;
}

@testable
class MyTestableClass {
  // ...
}

MyTestableClass.isTestable // true

注意:

修饰器函数的第一个参数,就是所要修饰的目标类

如果有多余参数,只能修饰器外面封装一层函数

function testable(isTestable) {
  return function(target) {
    target.isTestable = isTestable;
  }
}

@testable(true)
class MyTestableClass {}
MyTestableClass.isTestable // true

@testable(false)
class MyClass {}
MyClass.isTestable // false

2、方法的修饰

例如:

@bind

修饰器只能用于类和类的方法,不能用于函数,因为存在函数提升。

二、高阶组件

高阶函数:接受函数作为输入,输出一个新的函数
高阶组件:接受react组件作为输入,输出一个新的react组件
通俗讲:在普通组件外面包一层逻辑,就是高阶组件
将功能相同或者相似的代码提取出来封装成一个可公用的函数或者对象

import React from ‘react‘;

export default function withHeader(WrappedComponent) {
  return class HOC extends React.Component {
    static displayName = `HOC(${WrappedComponent.displayName || WrappedComponent.name})`

    state = {}

    render() {
      return (
        <div>
          <div className="demo-header">
            我是标题
          </div>
          <WrappedComponent {...this.props} />
        </div>
      );
    }
  };
}
import React from ‘react‘;
import withHeader from ‘../decorator‘;

@withHeader
export default class Demo extends React.Component {
  render() {
    return (
      <div>
        我是一个普通组件
      </div>
    );
  }
}

父组件和高阶组件区别:
高阶组件:可以封装公共逻辑,给当前组件传递方法属性,添加生命周期钩子,改善目前代码里业务逻辑和UI逻辑混杂在一起的现状
父组件:UI层的一些抽离

原文地址:https://www.cnblogs.com/alisadream/p/10209316.html

时间: 2024-08-28 18:18:18

修饰器&高阶组件的相关文章

高阶组件简介

组件:class Hello extends React.Component{ render(){ return <h2>hello imooc i love react&redux</h2> } } 组件本质其实就是一个函数. 高阶组件: function WrapperHello(Comp){ class WrapComp extends React.Component{ render(){ return( <div> <p>这是高阶组件特有的元

高阶组件装饰器

高阶组件装饰器  注意利用函数式组件进行化简! import React from 'react'; //1 组件原型 class Reg extends React.Component{ render(){ return <_Reg service={service} />; } } //2 匿名组件 const Reg = class extends React.Component{ render(){ return <_Reg service={service} />; }

高阶组件

前言 本文代码浅显易懂,思想深入实用.此属于react进阶用法,如果你还不了解react,建议从文档开始看起. 我们都知道高阶函数是什么, 高阶组件其实是差不多的用法,只不过传入的参数变成了react组件,并返回一个新的组件. A higher-order component is a function that takes a component and returns a new component. 形如: const EnhancedComponent = higherOrderComp

react高阶组件的理解

[高阶组件和函数式编程] function hello() { console.log('hello jason'); } function WrapperHello(fn) { return function() { console.log('before say hello'); fn(); console.log('after say hello'); } } // hello 这时候等于 WrapperHello函数中返回的 匿名函数 // 在设计模式中这种操作叫做 装饰器模式 // 高

【转】react的高阶组件

前言 本文代码浅显易懂,思想深入实用.此属于react进阶用法,如果你还不了解react,建议从文档开始看起. 我们都知道高阶函数是什么, 高阶组件其实是差不多的用法,只不过传入的参数变成了react组件,并返回一个新的组件. A higher-order component is a function that takes a component and returns a new component. 形如: const EnhancedComponent = higherOrderComp

React17React高阶组件的链式调用

使用情况如下: * 编写一个高阶组件进行属性的添加 * 编写一个高级组件编写生命周期 * 然后将以上两个高阶组件进行链式调用 示例 * 编写高阶组件进行属性的添加(返回的是函数组件): * 编写高阶组件重写生命周期(返回的是class组件): *链式调用 widthLearnReact为HOC组件添加了属性,widthLiseCycle为添加了属性的HOC组件重写了componentDidMount生命周期 注这种高阶组件链式调用太过麻烦,下篇随笔采用装饰器的语法处理这种问题. 原文地址:htt

react--context,高阶组件,hook

1,Context:不需要在任何组件间添加props方法,可以任意之间的组件树进行 const Context = React.createContext(); const Provider = Context.Provider; //提供者 const Consumer = Context.Consumer; //消费者 function App() { return ( <div className="App"> {/* <HomePag {...store} /

聊聊React高阶组件(Higher-Order Components)

使用 react已经有不短的时间了,最近看到关于 react高阶组件的一篇文章,看了之后顿时眼前一亮,对于我这种还在新手村晃荡.一切朝着打怪升级看齐的小喽啰来说,像这种难度不是太高同时门槛也不是那么低的东西如今可不多见了啊,是个不可多得的 zhuangbility的利器,自然不可轻易错过,遂深入了解了一番. 概述 高阶组件的定义 React 官网上对高阶组件的定义: 高阶部件是一种用于复用组件逻辑的高级技术,它并不是 React API的一部分,而是从React 演化而来的一种模式. 具体地说,

Mixins 改成使用高阶组件调用

把组件放在另外一个组件的 render 方法里面, 并且利用了 {...this.props} {...this.state} 这些  JSX 展开属性 对比下2种代码: 原始方式: <!DOCTYPE html> <html> <head> <script src="../build/react.js"></script> <script src="../build/react-dom.js">