ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

React-Native基础语法记录

2026/9/24 6:12:17 拓冰建站 浏览量
React-Native基础语法记录

1、基于flex布局,默认是纵向排列

flex-direction: column;

2、width,height等设置不需要单位(px,vw,vh...),可以采用百分比


3、样式不继承,例如:子元素不会父元素的fontSize


4、View标签不能单独存放文本


5、Imgae标签引用的外部资源图片,必须要设置width,height属性


6、transform写法,以数组的形式

// 向下移动100,放大 2 倍
style={{transform: [{translateY: 100},{scale: 2}]}}

7、引用react-native的Dimensions方法可以获取设备宽高

import {Dimensions} from 'react-native'
// math.round()是 Javascript中的一个内置函数,它的作用是对一个数字进行四舍五入取整
const screenWidth = Math.round(Dimensions.get('width').width)
const screenHeight = Math.round(Dimensions.get('height').height)

8、mobx 全局数据管理库


9、mobx-react: 方便在react中使用mobx

// mobx.js
import {observable, action} from 'mobx';
class RootStore {// es7装饰器语法 Object.defineProerty@observablename: '';// 修改全局变量的方法@actionchangeName(name){this.name = name}
}export default new RootStore();// 在根组件中引入
import {Provider} from 'mobx-react';
import RootStore from './mobx'
<Provider RootStore={RootStore}>// 业务组件
</Provider>// 业务组件中,通过props即可引用
import {inject, observer} from 'mobx-react'
@inject('RootStore') // 引入
@observer // 监听数据变化更新dom//然后通过this.props.RootStore即可获取