react高阶组件:如何同时兼容class类组件和函数式组件。

场景:

每个页面都要实现分享功能,但是页面有些是用class类,有些又直接是函数式。

方案1: 写2套方法。各自引用。(维护不太好,改要改2遍)

方案2: 可以封一个 jsx的组件,假如名为<Share>。返回一个<><>,在每个页面render或者 return里面引用<Share>

方案3:目前在用的,直接贴代码:

通过判断组件的类型,继承不同的原型。

/*
 * @Date: 2024-03-04 16:10:22
 * @Description: 右上角分享的钩子函数
 */

import React from 'react'

export function isClassComponent(component) {
  return (
    typeof component === 'function' && !!component.prototype.isReactComponent
  )
}

export default function withShare(Component) {
  const classComponent = class ContainerBox extends Component {
    /** 分享到聊天框 */
    onShareAppMessage() {
      const url = '/pages/home/index'
      const title = 'title'
      const shareData: any = {
        title: this.props?.title || title,
        path: this.props?.url || url,
        imageUrl: $.cdn('ypdj_mini_share.png'),
      }
      if (this.props?.img) {
        shareData.imageUrl = this.props.img
      }
      return shareData
    }

    /** 分享到朋友圈 */
    onShareTimeline() {
      const title = '全国家居售后服务平台'
      const shareData: any = {
        title: this.props?.title || title,
      }
      if (this.props?.img) {
        shareData.imageUrl = this.props.img
      }
      return shareData
    }
  }

  const functionComponent = class ContainerBox extends React.PureComponent<any> {
    /** 分享到聊天框 */
    onShareAppMessage() {
      const url = '/pages/home/index'
     const title = 'title'
      const shareData: any = {
        title: this.props?.title || title,
        path: this.props?.url || url,
        imageUrl: $.cdn('ypdj_mini_share.png'),
      }
      if (this.props?.img) {
        shareData.imageUrl = this.props.img
      }
      return shareData
    }

    /** 分享到朋友圈 */
    onShareTimeline() {
      const title = 'title'
      const shareData: any = {
        title: this.props?.title || title,
      }
      if (this.props?.img) {
        shareData.imageUrl = this.props.img
      }
      return shareData
    }

    render(): React.ReactNode {
      return (
        <Component {...this.props}></Component>
      )
    }
  }

  if (isClassComponent(Component)) {
    return classComponent
  }
  return functionComponent
}

相关推荐

  1. react函数组件组件

    2024-03-12 05:06:03       61 阅读
  2. React----函数组件组件

    2024-03-12 05:06:03       54 阅读
  3. React中的函数组件组件的区别

    2024-03-12 05:06:03       30 阅读
  4. React组件详解

    2024-03-12 05:06:03       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-12 05:06:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-12 05:06:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-12 05:06:03       87 阅读
  4. Python语言-面向对象

    2024-03-12 05:06:03       96 阅读

热门阅读

  1. 如何防范企业内部安全威胁?

    2024-03-12 05:06:03       33 阅读
  2. Minio docker容器化部署配置详情

    2024-03-12 05:06:03       42 阅读
  3. 二叉树的层序遍历

    2024-03-12 05:06:03       42 阅读
  4. 服务器访问慢怎么办

    2024-03-12 05:06:03       39 阅读
  5. 【力扣100】198.打家劫舍

    2024-03-12 05:06:03       41 阅读
  6. springboot参数传递总结

    2024-03-12 05:06:03       36 阅读
  7. json 基本上面试题目比较常问

    2024-03-12 05:06:03       43 阅读
  8. 大数据笔记

    2024-03-12 05:06:03       36 阅读
  9. C 语言中 #define 预处理器指令

    2024-03-12 05:06:03       38 阅读