美文网首页
HOC高阶组件

HOC高阶组件

作者: zhang_pan | 来源:发表于2024-04-08 14:58 被阅读0次

什么是高阶函数?

如果一个函数接受的参数为函数,或者返回值是一个新函数,则该函数就是高阶函数。

setTimeout(() => {}, 1000);
array.filter((item, index) => item === target);
Promise...

什么是高阶组件?

如果一个组件的参数是组件,返回值是一个新组件,则该组件就是高阶组件。

高阶组件应用场景:解决了什么问题?

使用HOC高阶组件解决横切关注点问题,使得组件功能更加单一,组件逻辑服务组件UI,从而降低耦合性,增强组件的复用性。

高阶组件使用场景1

Hack渲染函数:首页添加按钮

image.d.ts

declare module '*.png'

withFloatButton.tsx

import { Image, StyleSheet, TouchableOpacity } from "react-native";
import add from '../assets/images/icon_add.png';

type IReactComponent = 
    React.ClassicComponentClass
    | React.ComponentClass
    | React.FunctionComponent
    | React.ForwardRefExoticComponent<any>;

export default <T extends IReactComponent> (OriginView: T): T =>  {
    const HOCView = (props: any) => {
        return (
            <>
                <OriginView {...props} />
                <TouchableOpacity 
                    style={styles.imgButton}
                    onPress={() => {
                        console.log('onPress...')
                    }}
                >
                    <Image
                        style={styles.img}
                        source={add}
                    >

                    </Image>
                </TouchableOpacity>
            </>
        );
    }
    return HOCView as T;
}

const styles = StyleSheet.create({
    imgButton: {
        position: 'absolute',
        right: 24,
        bottom: 60
    },
    img: {
        width: 50,
        height: 50,
        resizeMode: 'contain'
    }
})

InfoView.js

import React, { useEffect } from 'react';
import {
  StyleSheet,
  View,
  Image,
  Text,
} from 'react-native';

import icon_avatar from '../assets/images/default_avatar.png';
import withFloatButton from './withFloatButton';

export default withFloatButton(() => {

    const styles = darkStyles;
    return (
        <View style={styles.content}>
            <Image style={styles.img} source={icon_avatar} />
            <Text style={styles.txt}>个人信息介绍</Text>
            <View style={styles.infoLayout}>
                <Text style={styles.infoTxt}>
                    各位产品经理大家好,我是个人开发者张三,我学习RN两年半了,我喜欢安卓、RN、Flutter,Thank you!。
                </Text>
            </View>
        </View>
    );
});

const darkStyles = StyleSheet.create({
    content: {
        width: '100%',
        height: '100%',
        backgroundColor: '#353535',
        flexDirection: 'column',
        alignItems: 'center',
        paddingHorizontal: 16,
        paddingTop: 64,
    },
    img: {
        width: 96,
        height: 96,
        borderRadius: 48,
        borderWidth: 4,
        borderColor: '#ffffffE0',
    },
    txt: {
        fontSize: 24,
        color: 'white',
        fontWeight: 'bold',
        marginTop: 32,
    },
    infoLayout: {
        width: '90%',
        padding: 16,
        backgroundColor: '#808080',
        borderRadius: 12,
        marginTop: 24,
    },
    infoTxt: {
        fontSize: 16,
        color: 'white',
    },
});

高阶组件使用场景2

Hack生命周期:首页上报设备信息

import { Image, StyleSheet, TouchableOpacity } from "react-native";
import add from '../assets/images/icon_add.png';
import { useEffect } from "react";

type IReactComponent = 
    React.ClassicComponentClass
    | React.ComponentClass
    | React.FunctionComponent
    | React.ForwardRefExoticComponent<any>;

export default <T extends IReactComponent> (OriginView: T): T =>  {
    const HOCView = (props: any) => {
        useEffect(() => {
            reportDeviceInfo();
        }, []);

        const reportDeviceInfo = () => {
            const DeviceInfo = {
                id: 1,
                model: '',
                storage: '',
            }
            //模拟上报
        }

        return (
            <>
                <OriginView {...props} />
                <TouchableOpacity 
                    style={styles.imgButton}
                    onPress={() => {
                        console.log('onPress...')
                    }}
                >
                    <Image
                        style={styles.img}
                        source={add}
                    >

                    </Image>
                </TouchableOpacity>
            </>
        );
    }
    return HOCView as T;
}

const styles = StyleSheet.create({
    imgButton: {
        position: 'absolute',
        right: 24,
        bottom: 60
    },
    img: {
        width: 50,
        height: 50,
        resizeMode: 'contain'
    }
})

高阶组件使用思考

  1. 不要改变原始组件的原型
  2. 必要的话可以传多个参数

相关文章

  • React 进阶之高阶组件

    高阶组件 HOC 高阶组件(HOC)是react中的高级技术,用来重用组件逻辑。但高阶组件本身并不是React A...

  • ReactNative中的高阶组件(HOC)和继承详解

    ReactNative中的高阶组件(HOC)和继承详解 共同点: 高阶组件(HOC)是 React 中用于复用组件...

  • 高阶组件

    Hoc(高阶组件) 概念 hoc基本用法 hoc链式调用 hoc装饰器用法 概念 概念: 接受组件, 返回新组件,...

  • React 高阶组件(HOC)

    什么是高阶组件? 高阶组件(Higher-Order Components,简称HOC):简而言之,高阶组件就是加...

  • React高阶组件(HOC)

    高阶组件(Higher-Order Components) 高阶组件(HOC)是 React 中用于重用组件逻辑的...

  • 高阶组件

    React 高阶组件HOC (Higher-Order Component) 高阶组件是react中重复使用组件逻...

  • React进阶篇(一)高阶组件

    高阶组件 高阶组件(Higher Order Component,HOC)是React的一种设计模式,用于增强现有...

  • 从高阶函数到高阶组件

    介绍 高阶组件(HOC)是 React 中用于复用组件逻辑的一种高级技巧。HOC 自身不是 React API 的...

  • React 学习笔记二 - HOC 高阶组件理解

    官方定义 高阶组件(HOC)是 React 中用于 复用组件逻辑 的一种高级技巧。HOC 自身不是 React A...

  • 高阶组件

    higher-order-component (高阶组件HOC)类似于高阶函数,它接受一个React组件作为参数,...

网友评论

      本文标题:HOC高阶组件

      本文链接:https://www.haomeiwen.com/subject/lqtxxjtx.html