RN | class 组件和函数式组件

class 组件

特点

  • 有状态(state),每次都是修改同一个状态
  • 基于生命周期管理
  • 面向对象
jsx 复制代码
/* eslint-disable react-native/no-inline-styles */
import React from 'react';
import {View} from 'react-native';

export class ClassView extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return <View style={{width: 200, height: 200, backgroundColor: 'blue'}} />;
  }
}

生命周期

constructor->render->componentDidMount->componentWillUnmount(当组件即将从 DOM 中移除时会触发)

props 传递

jsx 复制代码
/* eslint-disable react-native/no-inline-styles */
import React from 'react';
import {Text, View} from 'react-native';

export class ClassView extends React.Component {
  constructor(props) {
    console.log('constructor...');
    super(props);
  }

  componentDidMount() {
    console.log('componentDidMount...');
  }

  componentWillUnmount() {
    console.log('componentWillUnmount...');
  }

  render() {
    console.log('render...');
    const {name, age, gender} = this.props;
    return (
      <View
        style={{
          width: '100%',
          height: 200,
          backgroundColor: 'blue',
        }}>
        <Text style={{color: '#fff'}}>{name}</Text>
        <Text style={{color: '#fff'}}>{age}</Text>
      </View>
    );
  }
}

class 组件内部的 state

jsx 复制代码
/* eslint-disable react-native/no-inline-styles */
import React from 'react';
import {Text, View} from 'react-native';

export class ClassView extends React.Component {
  constructor(props) {
    console.log('constructor...');
    super(props);
    this.state = {
      showDetail: true,
    };
  }

  componentDidMount() {
    console.log('componentDidMount...');

    // 5s后隐藏具体信息
    setTimeout(() => {
      this.setState({
        showDetail: false,
      });
    }, 5000);
  }

  render() {
    console.log('render...');
    const {name, age, gender} = this.props;
    return (
      <View
        style={{
          width: '100%',
          height: 200,
          backgroundColor: 'blue',
        }}>
        {this.state.showDetail && (
          <>
            <Text style={{color: '#fff'}}>{name}</Text>
            <Text style={{color: '#fff'}}>{age}</Text>
            <Text style={{color: '#fff'}}>{gender}</Text>
          </>
        )}
      </View>
    );
  }
}

函数式组件和hook

特点

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

hooks

  • useState
  • useRef
  • useWindowDimensions
  • useColorScheme

useWindowDimensions

useWindowDimensions 是一个用于获取设备窗口尺寸的 Hook,它会在窗口大小发生变化时自动更新。这对于响应式布局特别有用。

useWindowDimensions 返回一个对象,包含以下属性:

ts 复制代码
interface WindowDimensions {
  width: number;       // 窗口宽度
  height: number;      // 窗口高度
  scale: number;       // 设备像素比
  fontScale: number;   // 字体缩放比例
}

useColorScheme

用于获取用户设备当前的颜色主题偏好(深色或浅色模式)

useColorScheme() 返回值可能是:

  • 'light' - 浅色模式
  • 'dark' - 深色模式
  • null - 未知或不支持

PS:

  • 需要在 React Native 0.63 或更高版本中使用
  • iOS 13+ 和 Android 10+ 才能完全支持

例子:

jsx 复制代码
import { useColorScheme } from 'react-native';

function MyComponent() {
  const colorScheme = useColorScheme();
  
  return (
    <View style={{
      backgroundColor: colorScheme === 'dark' ? '#000' : '#fff',
      // ...
    }}>
      <Text style={{
        color: colorScheme === 'dark' ? '#fff' : '#000',
      }}>
        当前主题: {colorScheme}
      </Text>
    </View>
  );
}
相关推荐
乐多_L44 分钟前
使用vue3框架vue-next-admin导出表格excel(带图片)
前端·javascript·vue.js
南望无一1 小时前
React Native 0.70.x如何从本地安卓源码(ReactAndroid)构建
前端·react native
Mike_188702783511 小时前
1688代采下单API接口使用指南:实现商品采集与自动化下单
前端·python·自动化
鲨鱼辣椒️面1 小时前
HTML视口动画
前端·html
一小路一1 小时前
Go Web 开发基础:从入门到实战
服务器·前端·后端·面试·golang
堇舟1 小时前
HTML第一节
前端·html
私人珍藏库1 小时前
[Android] APK提取器(1.3.7)版本
android
纯粹要努力1 小时前
前端跨域问题及解决方案
前端·javascript·面试
小刘不知道叫啥1 小时前
React源码揭秘 | 启动入口
前端·react.js·前端框架
m0_748232641 小时前
mysql的主从配置
android·mysql·adb