当前位置: 首页 > 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.dtcms.com/a/267285.html

相关文章:

  • 百度开源文心一言4.5:论文解读和使用入门
  • 闲庭信步使用SV搭建图像测试平台:第三十二课——系列结篇语
  • 【学习笔记】MySQL技术内幕InnoDB存储引擎——第5章 索引与算法
  • MySQL(118)如何使用SSL进行加密连接?
  • 前端进阶之路-从传统前端到VUE-JS(第三期-VUE-JS配套UI组件的选择)(Element Plus的构建)
  • vscode remote-ssh 拓展免密访问 linux虚拟机
  • 二分查找,乘法口诀表,判断闰年,判断素数,使用函数实现数组操作
  • CSS02:四种CSS导入方式
  • 动手实践OpenHands系列学习笔记7:前端界面设计
  • Flyway 介绍以及与 Spring Boot 集成指南
  • CppCon 2018 学习:Surprises In Object Lifetime
  • Linux systemd 服务启动失败Main process exited, code=exited, status=203/EXEC
  • xformers--Transformer优化加速器使用
  • 暑假算法日记第一天
  • App爬虫工具篇-appium配置
  • Spring Boot中POST请求参数校验的实战指南
  • bean注入的过程中,Property of ‘java.util.ArrayList‘ type cannot be injected by ‘List‘
  • 虚拟机网络编译器还原默认设置后VMnet8和VMnet1消失了
  • 第三方软件测试费用受啥影响?规模和测试类型了解下?
  • Python 训练营打卡 Day 53-对抗生成网络
  • Linux关机指令详解:shutdown命令的使用指南
  • Linux:多线程---深入互斥浅谈同步
  • 动手实践OpenHands系列学习笔记5:代理系统架构概述
  • java中,stream的filter和list的removeIf筛选速度比较
  • 力扣网编程55题:跳跃游戏之逆向思维
  • 虚拟机与容器技术详解:VM、LXC、LXD与Docker
  • 【内存】Linux 内核优化实战 - net.ipv4.tcp_max_tw_buckets
  • [创业之路-474]:企业经营层 - 小米与华为多维对比分析(2025年视角),以后不要把这两家公司放在同一个维度上 进行比较了
  • Springboot应用WebSocket服务测试
  • 软著难不难,申请