高阶组件介绍


高阶组件约定俗成以with开头

复制代码
import React, { useEffect } from 'react';
import { TouchableOpacity, Image, StyleSheet } from 'react-native';

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

import icon_add from '../assets/images/icon_add.png';

export default <T extends IReactComponent>(OriginView: T, type: string): T => {

    const HOCView = (props: any) => {

        useEffect(() => {
            reportDeviceInfo();
        }, []);

        const reportDeviceInfo = () => {
            // 模拟上报
            const deviceInfo = {
                deviceId: 1,
                deviceName: '',
                modal: '',
                storage: 0,
                ip: '',
            };

            // reportDeviceInfo(deviceInfo);
        }

        return (
            <>
                <OriginView {...props} />
                <TouchableOpacity
                    style={styles.addButton}
                    onPress={() => {
                        console.log(`onPress ...`);
                    }}
                >
                    <Image style={styles.addImg} source={icon_add} />
                </TouchableOpacity>
            </>
        );
    }

    return HOCView as T;
}

const styles = StyleSheet.create({
    addButton: {
        position: 'absolute',
        bottom: 80,
        right: 28,
    },
    addImg: {
        width: 54,
        height: 54,
        resizeMode: 'contain',
    },
});

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>
    );
}, 'InfoView');

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',
    },
});
相关推荐
roman_日积跬步-终至千里5 分钟前
【Java 并发-面试】从线程基础到企业级开发的知识点概况
java·开发语言
云中飞鸿5 分钟前
VS2015安装后,安装QT59,之后安装qt-vsaddin-msvc2015-2.4.3.vsix 文件失败问题!
开发语言·qt
m0_748233178 分钟前
C与C++:底层编程的六大核心共性
java·开发语言
沐知全栈开发11 分钟前
HTTP Content-Type
开发语言
一切尽在,你来21 分钟前
C++多线程教程-1.2.2 C++标准库并发组件的设计理念
开发语言·c++
m0_5613596731 分钟前
代码热更新技术
开发语言·c++·算法
2601_9498333934 分钟前
flutter_for_openharmony口腔护理app实战+知识实现
android·javascript·flutter
兩尛37 分钟前
c++知识点1
java·开发语言·c++
凯子坚持 c37 分钟前
Qt常用控件指南(9)
开发语言·qt
ONE_PUNCH_Ge38 分钟前
Go 语言泛型
开发语言·后端·golang