当前位置:首页 > 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(类组件)访问。

react native 如何写组件

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

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

react native 如何写组件

样式处理

使用 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
封装可复用的逻辑:

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 组件可以通过多种方式实现,以下是常见的几种方法: 单文件组件 (SFC) 使用 .vue 文件,包含模板、脚本和样式三部分: <template>…

uniapp组件

uniapp组件

uniapp组件基础概念 uniapp的组件系统基于Vue.js,支持跨平台开发(H5、小程序、App等)。组件分为内置基础组件(如<view>、<button>)和自定义组件…

vue 实现组件刷新

vue 实现组件刷新

组件局部刷新 在Vue中实现组件刷新可以通过强制重新渲染组件来实现。常用的方法有以下几种: 使用v-if指令 通过切换v-if条件触发组件的销毁和重建 <template> <…

vue实现弹窗组件

vue实现弹窗组件

实现弹窗组件的基本结构 在Vue中创建一个弹窗组件通常需要三个核心部分:组件模板、样式和逻辑控制。弹窗组件应具备打开、关闭功能,并支持内容插槽或属性传入。 <template> &l…

vue实现高阶组件

vue实现高阶组件

Vue 高阶组件实现方法 高阶组件(HOC)是一种复用组件逻辑的模式,通过函数包裹组件并返回新组件实现。Vue 中可通过以下方式实现: 使用 render 函数 通过函数接收组件选项并返回新组件选项…

vue实现组件刷新

vue实现组件刷新

强制重新渲染组件 使用 this.$forceUpdate() 方法可以强制 Vue 实例重新渲染,但不会更新子组件。适用于依赖非响应式数据的情况。 methods: { refreshComp…