当前位置:首页 > 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>
    );
  }
}

组件生命周期

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

react 组件实现

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>;
}

组件样式处理

内联样式:

react 组件实现

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 可以提取组件逻辑以便复用:

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 radio组件实现

vue radio组件实现

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

vue实现穿梭框树组件

vue实现穿梭框树组件

Vue 穿梭框树组件实现 穿梭框树组件通常结合了树形结构和穿梭框功能,允许用户在左右两栏之间移动树节点数据。以下是基于 Vue 的实现方案: 核心功能设计 数据结构 树形数据通常采用嵌套结构,例如:…

如何降低react版本

如何降低react版本

降低 React 项目版本的步骤 检查当前 React 版本 运行以下命令查看项目中安装的 React 当前版本: npm list react 或 yarn list react 修改…

react如何运行

react如何运行

运行React项目的步骤 安装Node.js 确保系统已安装Node.js(建议版本12以上),可从官网下载并安装。Node.js自带npm包管理器,用于后续依赖安装。 创建React项目 使用官方…

如何构建react

如何构建react

构建 React 项目的步骤 安装 Node.js 和 npm 确保系统已安装 Node.js(包含 npm)。可通过命令行验证: node -v npm -v 使用 Create React Ap…

react如何清理

react如何清理

清理 React 项目的方法 清理未使用的依赖项 运行 npm prune 或 yarn install --production 可以移除 node_modules 中未在 package.json…