当前位置:首页 > React

react如何写一个纯ui组件

2026-01-26 09:41:25React

编写纯UI组件的核心原则

纯UI组件(又称"无状态组件"或"展示组件")应只关注界面渲染,不包含业务逻辑或状态管理。这类组件通过props接收数据,通过回调函数与父组件通信。

函数组件实现方式

使用函数组件是最简洁的写法,适合简单的UI展示:

const Button = ({ text, onClick, disabled = false }) => (
  <button 
    onClick={onClick}
    disabled={disabled}
    className="primary-btn"
  >
    {text}
  </button>
);

类组件实现方式

当需要生命周期方法时可以使用类组件:

class Modal extends React.Component {
  render() {
    const { title, content, onClose } = this.props;
    return (
      <div className="modal-overlay">
        <div className="modal-content">
          <h3>{title}</h3>
          <p>{content}</p>
          <button onClick={onClose}>Close</button>
        </div>
      </div>
    );
  }
}

类型检查(PropTypes)

为组件添加prop类型检查能提高可维护性:

react如何写一个纯ui组件

import PropTypes from 'prop-types';

const Avatar = ({ src, size, alt }) => (
  <img 
    src={src} 
    width={size} 
    height={size} 
    alt={alt} 
  />
);

Avatar.propTypes = {
  src: PropTypes.string.isRequired,
  size: PropTypes.number,
  alt: PropTypes.string
};

Avatar.defaultProps = {
  size: 40,
  alt: 'User avatar'
};

样式处理方案

常见的样式处理方式有三种:

CSS Modules

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

const Card = ({ children }) => (
  <div className={styles.card}>
    {children}
  </div>
);

Styled-components

react如何写一个纯ui组件

import styled from 'styled-components';

const StyledInput = styled.input`
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
`;

const TextInput = ({ value, onChange }) => (
  <StyledInput 
    type="text" 
    value={value} 
    onChange={onChange} 
  />
);

内联样式

const Badge = ({ color, text }) => {
  const style = {
    backgroundColor: color,
    padding: '2px 6px',
    borderRadius: '10px'
  };

  return <span style={style}>{text}</span>;
};

最佳实践建议

  • 保持组件单一职责原则,每个组件只做一件事
  • 为可重用组件编写清晰的propTypes
  • 避免在展示组件中使用状态(useState/this.state)
  • 将事件处理通过props传递给父组件
  • 使用默认参数处理可选props
  • 为组件添加displayName便于调试

性能优化技巧

使用React.memo避免不必要的重渲染:

const MemoizedList = React.memo(({ items }) => (
  <ul>
    {items.map(item => (
      <li key={item.id}>{item.text}</li>
    ))}
  </ul>
));

对于类组件可以使用PureComponent:

class PureButton extends React.PureComponent {
  render() {
    return <button>{this.props.text}</button>;
  }
}

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

相关文章

vue如何实现目录组件

vue如何实现目录组件

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

react如何开发组件

react如何开发组件

React 组件开发基础 React 组件分为函数组件和类组件两种形式。函数组件是现代 React 开发的主流方式,结合 Hooks 可以实现完整功能。 函数组件示例: function Gree…

vue table组件实现

vue table组件实现

Vue 表格组件实现方案 基础表格实现 使用<table>标签结合v-for指令渲染数据是最简单的实现方式: <template> <table> &…

react如何删除组件

react如何删除组件

删除 React 组件的几种方法 条件渲染法 通过状态控制组件是否渲染。当状态为 false 时,组件会被移除: const [showComponent, setShowComponent…

vue实现父子组件

vue实现父子组件

Vue 父子组件通信实现方式 1. 父组件向子组件传递数据(Props) 父组件通过 props 向子组件传递数据,子组件通过 props 接收数据。 父组件模板: <template>…

vue实现分页组件

vue实现分页组件

Vue 分页组件实现 基础分页组件结构 创建一个名为 Pagination.vue 的文件,包含以下基础模板和逻辑: <template> <div class="pa…