高阶组件介绍


高阶组件约定俗成以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',
    },
});
相关推荐
郝学胜-神的一滴17 小时前
Linux下,获取子进程退出值和异常终止信号
linux·服务器·开发语言·c++·程序人生
AI科技星17 小时前
张祥前统一场论动量公式P=m(C-V)误解解答
开发语言·数据结构·人工智能·经验分享·python·线性代数·算法
是你的小橘呀17 小时前
深入理解 JavaScript 预编译:从原理到实践
前端·javascript
风止何安啊17 小时前
栈与堆的精妙舞剧:JavaScript 数据类型深度解析
前端·javascript
CodeByV17 小时前
【C++】继承
开发语言·c++
用户479492835691518 小时前
Chrome DevTools MCP:让 AI 助手直接操作浏览器开发工具
前端·javascript·chrome
Rysxt_18 小时前
Vuex 教程 从入门到实践
前端·javascript·vue.js
权泽谦18 小时前
用 Python 做一个天气预报桌面小程序(附源码 + 打包与部署指导)
开发语言·python·小程序
ftpeak18 小时前
《Rust+Slint:跨平台GUI应用》第八章 窗体
开发语言·ui·rust·slint
森语林溪18 小时前
大数据环境搭建从零开始(十七):JDK 17 安装与配置完整指南
java·大数据·开发语言·centos·vmware·软件需求·虚拟机