当前位置:首页 > React

react native 如何写组件

2026-01-25 01:29:10React

React Native 组件开发指南

React Native 组件开发主要分为两种类型:函数组件类组件。以下分别介绍两种组件的写法及核心概念。

函数组件写法

函数组件是 React Native 推荐的方式,使用 Hooks 实现状态管理和生命周期。

import React, { useState, useEffect } from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';

const MyFunctionalComponent = ({ initialValue }) => {
  const [count, setCount] = useState(initialValue || 0);

  useEffect(() => {
    console.log('Component mounted or count updated');
    return () => {
      console.log('Component will unmount');
    };
  }, [count]);

  return (
    <View style={styles.container}>
      <Text>Count: {count}</Text>
      <Button 
        title="Increment" 
        onPress={() => setCount(count + 1)} 
      />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    padding: 20,
    alignItems: 'center'
  }
});

export default MyFunctionalComponent;

类组件写法

类组件通过继承 React.Component 实现,适用于需要完整生命周期控制的场景。

import React from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';

class MyClassComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: props.initialValue || 0
    };
  }

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

  componentDidUpdate() {
    console.log('Component updated');
  }

  componentWillUnmount() {
    console.log('Component will unmount');
  }

  render() {
    return (
      <View style={styles.container}>
        <Text>Count: {this.state.count}</Text>
        <Button 
          title="Increment" 
          onPress={() => this.setState({ count: this.state.count + 1 })} 
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    padding: 20,
    alignItems: 'center'
  }
});

export default MyClassComponent;

组件核心特性

Props(属性)
通过父组件传递数据,组件内通过 props(函数组件)或 this.props(类组件)访问。

State(状态)
组件内部可变数据,函数组件用 useState,类组件用 this.statethis.setState

生命周期
函数组件通过 useEffect 模拟,类组件直接使用 componentDidMount 等方法。

样式处理

使用 StyleSheet.create 定义样式对象,支持 Flexbox 布局:

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center'
  },
  text: {
    fontSize: 16,
    color: '#333'
  }
});

组件复用模式

高阶组件(HOC)
包装现有组件扩展功能:

const withLogger = (WrappedComponent) => {
  return (props) => {
    useEffect(() => {
      console.log('Component rendered');
    }, []);
    return <WrappedComponent {...props} />;
  };
};

自定义Hooks
封装可复用的逻辑:

react native 如何写组件

const useCounter = (initialValue) => {
  const [count, setCount] = useState(initialValue);
  const increment = () => setCount(count + 1);
  return { count, increment };
};

最佳实践

  1. 优先使用函数组件和 Hooks
  2. 复杂组件拆分为多个子组件
  3. 样式与逻辑分离,避免内联样式
  4. 使用 PropTypes 或 TypeScript 定义 props 类型

标签: 组件如何写
分享给朋友:

相关文章

vue实现日历组件

vue实现日历组件

Vue 日历组件实现步骤 基础结构搭建 使用Vue 3的Composition API或Options API创建组件框架。核心结构包括年份/月份切换按钮、星期标题栏和日期网格。 <t…

vue实现搜索组件

vue实现搜索组件

Vue 实现搜索组件的方法 基础搜索组件实现 创建基础搜索组件需要包含输入框和搜索按钮。在Vue中可以使用v-model绑定输入值,通过@click或@keyup.enter触发搜索。 <te…

vue实现拖拽组件

vue实现拖拽组件

Vue 实现拖拽组件的方法 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue组件实现跳转

vue组件实现跳转

Vue 组件实现跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,以下是常见的几种方法: 使用 router-link 组件 router-link 是 Vue Router 提供的组件,用于…

实现弹幕的vue组件

实现弹幕的vue组件

实现弹幕的Vue组件 弹幕功能可以通过Vue组件实现,核心逻辑包括弹幕数据的动态渲染、动画控制以及交互处理。以下是实现弹幕组件的关键步骤和代码示例。 弹幕数据结构 弹幕数据通常包含内容、颜色、位置和…

vue实现吸顶组件

vue实现吸顶组件

实现吸顶组件的核心思路 通过监听滚动事件,判断目标元素距离顶部的距离,动态添加固定定位样式。Vue中可利用指令或组件化方式封装。 使用指令方式实现 创建自定义指令v-sticky,在元素滚动到顶部时…