React Native 亲切的组件们(函数式组件/class组件)和陌生的样式

写多了taro, 看见react native中的组件好亲切啊,几乎一模一样。

一、函数式组件 --- 常用

1)无状态,每次刷新都是生成一个新的状态

2)基于状态变化的管理

3)简洁,代码少,易于服用

javascript 复制代码
import React from "react";
import { View, Text } from 'react-native';

// 子组件 TestFunctionDemo.js
export default function TestFunctionDemo(props) {
    const { name, info: {age, sex} } = props

    const [address, setAddress] = useState('中国')

    useEffect(() => {
        // 第一次加载完成
        setTimeout(() => {
            setAddress('大陆')
        }, 2000)
    }, [])

    useEffect(() => {
        // 监听address
    }, [address])

    return (
        <View style={{height: 200, width: '80%', padding: 20, borderRadius: 10, backgroundColor: '#ccc'}}>
            <Text>我是子组件</Text>
            <Text>姓名:{ name }</Text>
            <Text>年龄:{ age }</Text>
            <Text>性别:{ sex }</Text>
            <Text>{`地址:${ address }`}</Text>
            { props.children}
        </View>
    )
}


// 简写
const TestFunctionDemo = (props) => {
	// ...
}
export default TestFunctionDemo

// 或
export default (props) => {
   // ...
}
二、class组件

1) 有状态state, 每次都是修改的同一个状态

2) 基于生命周期的管理

javascript 复制代码
// 子组件 TestClassDemo .js
class TestClassDemo extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            address: '中国'
        }
    }

    componentDidMount() {
        setTimeout(() => {
            this.setState({
                address: '大陆'
            })
        }, 2000)
    }

    render() {
        const { name, info: {age, sex} } = this.props
        const { address } = this.state
        return (
            <View style={{height: 200, width: '80%', padding: 20, borderRadius: 10, backgroundColor: '#ccc'}}>
                <Text>我是子组件</Text>
                <Text>姓名:{ name }</Text>
                <Text>年龄:{ age }</Text>
                <Text>性别:{ sex }</Text>
                <Text>{`地址:${ address }`}</Text>
                { this.props.children}
            </View>
        )
    }
}

export default TestClassDemo
三、父组件引入
javascript 复制代码
// 父组件app.js中引入
import TestFunctionDemo from './src/components/TestFunctionDemo';
import TestClassDemofrom './src/components/TestClassDemo';

// ...
const info = {
  age: 20,
  sex: 'nan'
}

// ...

<TestFunctionDemo 
  name="zhangsan" 
  info={info}>
  <Text>我是通过插槽加入的!</Text>
</TestFunctionDemo>

<TestClassDemo 
  name="zhangsan" 
  info={info}>
  <Text>我是通过插槽加入的!</Text>
</TestClassDemo>

以上是不是和react一模一样

四、样式

!!!这里和react不一样

javascript 复制代码
import React from "react";
import { View, Text, StyleSheet } from 'react-native';

// 子组件 TestFunctionDemo.js
export default (props) => {
   // ...

 	return (
        <View style={styles.box}>
        	// 多个样式用数组
            <Text style={[styles.textLine, styles.title]}>我是子组件</Text>
            <Text  style={styles.textLine}>姓名:{ name }</Text>
            // ...
        </View>
    )
}

// 样式
const styles = StyleSheet.create({
    box: {
        height: 200, 
        width: '80%', 
        padding: 20, 
        borderRadius: 10, 
        backgroundColor: '#ccc'
    },
    textLine: {
        fontSize: 18,
        color: 'blue'
    },
    title: {
        fontWeight: "bold"
    }
})
相关推荐
木西8 小时前
React Native DApp 开发全栈实战·从 0 到 1 系列(流动性挖矿-前端部分)
react native·web3·智能合约
小妖怪的夏天11 小时前
react native 出现 FATAL EXCEPTION: OkHttp Dispatcher
react native·react.js·okhttp
qczg_wxg1 天前
高阶组件介绍
开发语言·javascript·react native·ecmascript
qczg_wxg3 天前
React Native的动画系统
javascript·react native·react.js
qczg_wxg3 天前
React Native常用的API
react native
Winson℡3 天前
在 React Native 层禁止 iOS 左滑返回(手势返回/手势退出)
react native·react.js·ios
卸任3 天前
从0到1搭建react-native自动更新(OTA和APK下载)
前端·react native·react.js
wayne2143 天前
「原生 + RN 混合工程」一条命令启动全攻略:解密 react-native.config.js
android·react native
qczg_wxg4 天前
ReactNative系统组件四
javascript·react native·react.js
木西6 天前
React Native DApp 开发全栈实战·从 0 到 1 系列(NFT交易-前端部分)
前端·react native