当前位置:首页 > 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 radio组件实现

vue radio组件实现

Vue Radio 组件实现 在 Vue 中实现 Radio 组件可以通过原生 HTML 的 <input type="radio"> 结合 Vue 的响应式特性,或者使用第三方 UI 库…

vue实现水印组件

vue实现水印组件

Vue 水印组件实现 基础水印组件实现 创建一个基础水印组件,通过 props 接收文本、颜色、大小等参数: <template> <div class="watermark…

vue哪个组件实现动画

vue哪个组件实现动画

在Vue中实现动画效果,可以通过以下组件和方法实现: Vue内置动画组件 Vue提供了<transition>和<transition-group>两个内置组件,用于处理进…

vue如何实现目录组件

vue如何实现目录组件

实现目录组件的基本思路 在Vue中实现目录组件通常需要结合页面内容的结构化数据(如标题层级),通过动态渲染生成可交互的目录。核心步骤包括提取标题、生成目录结构、实现滚动联动等。 提取标题信息 通过…

vue实现倒计时组件

vue实现倒计时组件

Vue 倒计时组件实现 核心思路 通过 setInterval 或 setTimeout 实现时间递减,结合 Vue 的响应式数据更新 UI。需注意组件销毁时清除定时器。 基础实现方案 模板部分…

uniapp组件写法

uniapp组件写法

Uniapp 组件的基本写法 Uniapp 的组件写法与 Vue 类似,支持单文件组件(.vue 文件)。一个基本的组件通常包含三个部分:<template>、<script>…