当前位置:首页 > React

react 组件实现

2026-01-26 12:15:20React

React 组件实现方式

React 组件是构建用户界面的核心单元,主要分为函数组件和类组件两种形式。以下是具体实现方法:

函数组件实现

函数组件是使用 JavaScript 函数定义的组件,简洁且易于理解:

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

箭头函数写法:

const Greeting = (props) => <h1>Hello, {props.name}</h1>;

类组件实现

类组件通过 ES6 的 class 定义,可以包含状态和生命周期方法:

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

组件状态管理

函数组件中使用 Hooks 管理状态:

import { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <button onClick={() => setCount(count + 1)}>
      Clicked {count} times
    </button>
  );
}

类组件中使用 state 对象:

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

  render() {
    return (
      <button onClick={() => this.setState({ count: this.state.count + 1 })}>
        Clicked {this.state.count} times
      </button>
    );
  }
}

组件生命周期

类组件中可以使用的生命周期方法示例:

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

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

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

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

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

import { useEffect } from 'react';

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

  return <div>Lifecycle Example</div>;
}

组件间通信

父组件向子组件传递 props:

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

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

子组件向父组件传递数据:

function Parent() {
  const handleData = (data) => {
    console.log('Received:', data);
  };
  return <Child onSendData={handleData} />;
}

function Child({ onSendData }) {
  return <button onClick={() => onSendData('Child data')}>Send</button>;
}

组件样式处理

内联样式:

function StyledComponent() {
  return <div style={{ color: 'red', fontSize: 20 }}>Styled Text</div>;
}

CSS Modules:

import styles from './Component.module.css';

function StyledComponent() {
  return <div className={styles.container}>Styled Text</div>;
}

Styled Components:

import styled from 'styled-components';

const StyledDiv = styled.div`
  color: red;
  font-size: 20px;
`;

function StyledComponent() {
  return <StyledDiv>Styled Text</StyledDiv>;
}

高阶组件实现

高阶组件(HOC)是用于组件逻辑复用的高级技术:

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

const EnhancedComponent = withLogging(MyComponent);

自定义 Hook 实现

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

react 组件实现

function useCounter(initialValue = 0) {
  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();
  return <button onClick={increment}>{count}</button>;
}

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

相关文章

vue组件传值实现分页

vue组件传值实现分页

Vue组件传值实现分页的方法 在Vue中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方式: 使用props传递数据 父组件通过props向子组件传递当前页码和总页数等分页信息:…

react如何取消渲染

react如何取消渲染

取消渲染的方法 在React中,取消渲染通常指的是在某些条件下阻止组件渲染或中断正在进行的渲染过程。以下是几种常见的方法: 条件渲染 通过条件判断决定是否渲染组件或部分内容。可以使用if语句或三元运…

如何手写一个react

如何手写一个react

手写一个简单的 React 创建一个基础的 React 实现需要理解其核心概念:虚拟 DOM、组件、状态管理和渲染。以下是一个简化版的 React 实现。 创建虚拟 DOM 虚拟 DOM 是真实 D…

react如何遍历

react如何遍历

遍历数组或对象的方法 在React中,遍历数组或对象通常用于渲染列表或动态生成内容。以下是几种常见的遍历方法: 使用map遍历数组map是遍历数组并返回新数组的高阶函数,适合渲染列表。 con…

react如何调度

react如何调度

React 调度机制概述 React 的调度机制通过 Fiber 架构 和 Scheduler 模块实现任务优先级管理与时间切片(Time Slicing),确保高优先级更新(如用户交互)能快速响应,…

vue实现折叠组件

vue实现折叠组件

实现折叠组件的基本思路 在Vue中实现折叠组件通常需要利用动态绑定和条件渲染。核心是通过控制一个布尔值状态来决定内容是否显示,并添加过渡动画提升用户体验。 基础实现方法 使用v-show或v-if控…