当前位置: 首页 > news >正文

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

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

一、函数式组件 — 常用

1)无状态,每次刷新都是生成一个新的状态
2)基于状态变化的管理
3)简洁,代码少,易于服用

import React from "react";
import { View, Text } from 'react-native';// 子组件 TestFunctionDemo.js
export default function TestFunctionDemo(props) {const { name, info: {age, sex} } = propsconst [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) 基于生命周期的管理

// 子组件 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.propsconst { address } = this.statereturn (<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
三、父组件引入
// 父组件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不一样

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"}
})

在这里插入图片描述

http://www.lryc.cn/news/581301.html

相关文章:

  • RabbitMQ 4.1.1初体验-队列和交换机
  • 快速掌握Python编程基础
  • 结构型智能科技的关键可行性——信息型智能向结构型智能的转变(修改提纲)
  • 小架构step系列05:Springboot三种运行模式
  • 黑马点评系列问题之基础篇p7 06初识redis无法在虚拟机查到图形化界面存进去的键
  • 运算方法和运算器补充
  • TCP协议概念和特性
  • AI Agent与Agentic AI原理与应用(下) - 主流Agent平台、框架与项目技术拆解
  • 编程中的英语
  • cocos 打包安卓
  • Rust与PyTorch实战:精选示例
  • 机器学习--实践与分析
  • python优先队列使用
  • NAT、代理服务、内网穿透
  • Ubuntu 22.04 修改默认 Python 版本为 Python3 笔记
  • C#使用开源框架NetronLight绘制流程图
  • C++------模板初阶
  • JS 网页全自动翻译v3.17发布,全面接入 GiteeAI 大模型翻译及自动部署
  • 2025年的前后端一体化CMS框架优选方案
  • 【大模型入门】访问GPT的API
  • 【Halcon】WPF 自定义Halcon显示控件完整流程与 `OnApplyTemplate` 未触发的根本原因解析!
  • day 60 python打卡
  • ffplay6 播放器关键技术点分析 1/2
  • Windows内核并发优化
  • rk3128 emmc显示剩余容量为0
  • 深度学习5(深层神经网络 + 参数和超参数)
  • 力扣网编程55题:跳跃游戏之逆向思维
  • 前端相关性能优化笔记
  • Python数据容器-list和tuple
  • 四、jenkins自动构建和设置邮箱