当前位置: 首页 > 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.xdnf.cn/news/1081711.html

相关文章:

  • 若 VSCode 添加到文件夹内右键菜单中显示(通过reg文件方式)
  • 盘式制动器的设计+说明书和CAD)【6张】+绛重
  • Redis性能优化
  • 权电阻网络DAC实现电压输出型数模转换Multisim电路仿真——硬件工程师笔记
  • 前端捕获异常的全面场景及方法
  • Linux操作系统之文件(三):缓冲区
  • 每天一个前端小知识 Day 21 - 浏览器兼容性与 Polyfill 策略
  • 【每天一个知识点】动态知识库
  • JxBrowser 8.9.0 版本发布啦!
  • chrome插件合集
  • vue/微信小程序/h5 实现react的boundary
  • 智能电动汽车系列 --- 车载软件开发思想与已有OEM现状碰撞
  • vue-39(为复杂 Vue 组件编写单元测试)
  • 设计模式(十)
  • 区块链技术核心组件及应用架构的全面解析
  • Dash 安装使用教程
  • 程序计数器(PC)是什么?
  • Linux入门篇学习——Linux 帮助手册
  • 版本控制器SVN
  • 基于区块链的物联网(IoT)安全通信与数据共享的典型实例
  • 三体融合实战:Django+讯飞星火+Colossal-AI的企业级AI系统架构
  • Abase和ByteKV存储方案对比
  • [C++] C++多重继承:深入解析复杂继承关系
  • 怎么更改cursor字体大小
  • github上部署自己的静态项目
  • XILINX Kintex 7系列FPGA的全局时钟缓冲器(BUFG)和区域时钟缓冲器(BUFR/BUFH)的区别
  • hello判断
  • WPF学习笔记(23)Window、Page与Frame、ViewBox
  • 「Java案例」鸡兔同笼问题
  • [Linux]内核如何对信号进行捕捉