当前位置:首页 > React

如何写react 组件

2026-02-12 10:04:53React

React 组件的基本写法

React 组件分为函数组件和类组件两种形式。以下是两种组件的写法示例:

函数组件

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

类组件

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

组件 Props 的使用

Props 是从父组件传递给子组件的数据。在组件内部通过 props(函数组件)或 this.props(类组件)访问:

function Greeting(props) {
  return <p>Hello, {props.userName}!</p>;
}

// 使用组件
<Greeting userName="Alice" />

组件 State 的管理

State 是组件内部的状态数据。函数组件使用 useState Hook,类组件使用 this.state

函数组件

如何写react 组件

import { useState } from 'react';

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

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

类组件

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

  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
        <button onClick={() => this.setState({ count: this.state.count + 1 })}>
          Increment
        </button>
      </div>
    );
  }
}

生命周期方法(类组件)

类组件可以使用生命周期方法,如 componentDidMountcomponentDidUpdate 等:

class Example extends React.Component {
  componentDidMount() {
    console.log('Component mounted');
  }

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

  render() {
    return <div>Lifecycle Example</div>;
  }
}

使用 Hooks(函数组件)

Hooks 是函数组件中用于状态管理和副作用处理的 API:

如何写react 组件

import { useState, useEffect } from 'react';

function Timer() {
  const [seconds, setSeconds] = useState(0);

  useEffect(() => {
    const interval = setInterval(() => {
      setSeconds(seconds => seconds + 1);
    }, 1000);

    return () => clearInterval(interval);
  }, []);

  return <div>Seconds: {seconds}</div>;
}

事件处理

React 事件处理使用驼峰命名法,如 onClickonChange

function Button() {
  const handleClick = () => {
    alert('Button clicked');
  };

  return <button onClick={handleClick}>Click Me</button>;
}

条件渲染

根据条件渲染不同内容:

function Greeting({ isLoggedIn }) {
  return isLoggedIn ? <p>Welcome back!</p> : <p>Please sign in.</p>;
}

列表渲染

使用 map 渲染列表:

function NumberList({ numbers }) {
  return (
    <ul>
      {numbers.map(number => (
        <li key={number}>{number}</li>
      ))}
    </ul>
  );
}

组件组合

通过组合多个组件构建复杂 UI:

function App() {
  return (
    <div>
      <Header />
      <Content />
      <Footer />
    </div>
  );
}

以上是 React 组件的基本写法和常见用法,可以根据实际需求选择函数组件或类组件,并结合 Hooks 和生命周期方法实现更复杂的功能。

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

相关文章

vue组件实现

vue组件实现

Vue 组件实现基础 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

如何设计react组件

如何设计react组件

设计 React 组件的核心原则 React 组件的设计需要遵循高内聚、低耦合的原则,确保组件功能独立且易于维护。组件的设计可以分为展示组件和容器组件两类,展示组件负责 UI 渲染,容器组件负责逻辑处…

vue实现组件缓存

vue实现组件缓存

Vue 组件缓存的实现方法 在 Vue 中实现组件缓存通常使用 <keep-alive> 内置组件,它可以缓存不活动的组件实例,避免重复渲染和销毁。 基本用法 <keep-aliv…

vue实现树形组件

vue实现树形组件

实现树形组件的基本思路 在Vue中实现树形组件通常涉及递归组件和动态数据渲染。核心是通过组件自身调用自身,逐层渲染嵌套的节点数据。 定义树形数据结构 树形数据通常是一个包含children属性的对象…

vue实现组件重叠

vue实现组件重叠

Vue 实现组件重叠的方法 在 Vue 中实现组件重叠通常需要结合 CSS 的定位属性(如 position、z-index)和 Vue 的动态渲染机制。以下是几种常见实现方式: 使用绝对定位(Ab…

vue布局组件实现

vue布局组件实现

Vue 布局组件实现方法 基础布局组件设计 使用 Vue 的单文件组件(SFC)创建一个基础的布局组件框架,通常包含<header>、<main>、<footer>…