React-Native基础语法记录

发布时间:2024年01月14日

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
    @observable
    name: '';

    // 修改全局变量的方法
    @action
    changeName(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即可获取

文章来源:https://blog.csdn.net/chen_ac/article/details/135585664
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。