当前位置:首页 > React

react实现组件

2026-01-26 13:24:16React

React 组件实现方法

React 组件是构建用户界面的独立模块,分为函数组件和类组件两种形式。以下是常见的实现方式:

函数组件

函数组件是最简单的 React 组件形式,使用 JavaScript/TypeScript 函数定义:

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

现代 React 开发推荐使用箭头函数和 ES6 语法:

const Welcome = ({ name }) => {
  return <h1>Hello, {name}</h1>;
};

类组件

类组件通过继承 React.Component 实现,支持生命周期方法和状态管理:

class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

状态管理

函数组件使用 useState Hook 管理状态:

react实现组件

import { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

类组件通过 this.statethis.setState 管理状态:

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

  render() {
    return (
      <div>
        <p>You clicked {this.state.count} times</p>
        <button onClick={() => this.setState({ count: this.state.count + 1 })}>
          Click me
        </button>
      </div>
    );
  }
}

生命周期方法

类组件可以使用完整的生命周期方法:

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = { date: new Date() };
  }

  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),
      1000
    );
  }

  componentWillUnmount() {
    clearInterval(this.timerID);
  }

  tick() {
    this.setState({
      date: new Date()
    });
  }

  render() {
    return (
      <div>
        <h1>Hello, world!</h1>
        <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}

函数组件使用 useEffect Hook 替代生命周期:

react实现组件

import { useState, useEffect } from 'react';

function Clock() {
  const [date, setDate] = useState(new Date());

  useEffect(() => {
    const timerID = setInterval(() => tick(), 1000);
    return () => clearInterval(timerID);
  }, []);

  function tick() {
    setDate(new Date());
  }

  return (
    <div>
      <h1>Hello, world!</h1>
      <h2>It is {date.toLocaleTimeString()}.</h2>
    </div>
  );
}

组件通信

父组件通过 props 向子组件传递数据:

function Parent() {
  return <Child message="Hello from parent" />;
}

function Child({ message }) {
  return <p>{message}</p>;
}

子组件通过回调函数向父组件通信:

function Parent() {
  const handleClick = (data) => {
    console.log('Data from child:', data);
  };

  return <Child onClick={handleClick} />;
}

function Child({ onClick }) {
  return (
    <button onClick={() => onClick('Some data')}>
      Click me
    </button>
  );
}

高阶组件

高阶组件(HOC)是接收组件并返回新组件的函数:

function withLogging(WrappedComponent) {
  return function(props) {
    console.log('Rendering:', WrappedComponent.name);
    return <WrappedComponent {...props} />;
  };
}

const EnhancedComponent = withLogging(MyComponent);

自定义 Hook

自定义 Hook 可以提取组件逻辑:

function useCounter(initialValue) {
  const [count, setCount] = useState(initialValue);

  const increment = () => setCount(count + 1);
  const decrement = () => setCount(count - 1);

  return { count, increment, decrement };
}

function Counter() {
  const { count, increment } = useCounter(0);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}

标签: 组件react
分享给朋友:

相关文章

vue哪个组件实现动画

vue哪个组件实现动画

在Vue中实现动画效果可以通过多种方式,以下是常用的组件和方法: Vue内置过渡组件 Vue提供了<transition>和<transition-group>组件,用于处理…

react实现vue

react实现vue

React 实现 Vue 功能 React 和 Vue 是两种不同的前端框架,但可以通过一些方法在 React 中实现 Vue 的特性。以下是几种常见 Vue 功能在 React 中的实现方式: 双…

vue实现组件循环

vue实现组件循环

Vue 实现组件循环的方法 在 Vue 中,可以通过 v-for 指令实现组件的循环渲染。以下是几种常见的实现方式: 使用 v-for 渲染数组 通过 v-for 遍历数组数据,动态生成组件列表:…

vue穿梭框组件实现

vue穿梭框组件实现

实现 Vue 穿梭框组件的基本思路 穿梭框(Transfer)组件通常用于在两个列表之间移动数据项。核心功能包括左侧列表、右侧列表、移动按钮(左移、右移、全选等)以及数据项的渲染与交互。 基础结构设…

vue组件实现

vue组件实现

Vue 组件实现 Vue 组件是 Vue.js 的核心特性之一,允许开发者将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种常见方式: 单文件组件(SFC) 单文件组件是 Vue…

react native如何启动

react native如何启动

如何启动 React Native 项目 安装 Node.js 和 npm 确保已安装 Node.js(建议版本 16 或更高)和 npm(Node.js 自带)。可通过以下命令检查版本: node…