react-human-made
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHuman Made React Standards
Human Made React组件规范
Component Style
组件风格
- Use functional components with hooks for most cases
- Class components only when required by external libraries
- Keep components focused and single-purpose
- Extract complex logic into custom hooks
- 大多数情况下使用带Hooks的函数式组件
- 仅在外部库要求时使用类组件
- 保持组件专注且单一职责
- 将复杂逻辑提取到自定义Hooks中
Semantic HTML
语义化HTML
- Use semantic HTML5 elements (,
<article>,<section>, etc.)<nav> - handlers only on focusable elements (buttons, links)
onClick - Ensure keyboard accessibility for interactive elements
- Use ARIA attributes when semantic HTML is insufficient
- 使用语义化HTML5元素 (,
<article>,<section>, etc.)<nav> - 处理器仅用于可聚焦元素(按钮、链接)
onClick - 确保交互式元素的键盘可访问性
- 当语义化HTML不足以满足需求时使用ARIA属性
Props and State
Props与状态
- Specify PropTypes for all component properties
- Prefer props over state; lift state up when needed
- Avoid storing state in DOM
- Use controlled components for form inputs
- 为所有组件属性指定PropTypes
- 优先使用Props而非状态;必要时提升状态
- 避免在DOM中存储状态
- 表单输入使用受控组件
Component Organization
组件组织
- Co-locate styles and tests with components
- One component per file for significant components
- Small helper components can share a file with their parent
- 将样式和测试文件与组件放在同一目录下
- 重要组件采用一个文件对应一个组件的方式
- 小型辅助组件可与其父组件共享同一个文件
Example Component
示例组件
jsx
import PropTypes from 'prop-types';
import { useState, useCallback } from 'react';
const UserCard = ( { user, onSelect } ) => {
const [ isExpanded, setIsExpanded ] = useState( false );
const handleToggle = useCallback( () => {
setIsExpanded( prev => ! prev );
}, [] );
return (
<article className="user-card">
<h3>{ user.name }</h3>
<button onClick={ handleToggle }>
{ isExpanded ? 'Collapse' : 'Expand' }
</button>
{ isExpanded && (
<div className="user-card__details">
<p>{ user.email }</p>
<button onClick={ () => onSelect( user.id ) }>
Select User
</button>
</div>
) }
</article>
);
};
UserCard.propTypes = {
user: PropTypes.shape( {
id: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
email: PropTypes.string.isRequired,
} ).isRequired,
onSelect: PropTypes.func.isRequired,
};
export default UserCard;jsx
import PropTypes from 'prop-types';
import { useState, useCallback } from 'react';
const UserCard = ( { user, onSelect } ) => {
const [ isExpanded, setIsExpanded ] = useState( false );
const handleToggle = useCallback( () => {
setIsExpanded( prev => ! prev );
}, [] );
return (
<article className="user-card">
<h3>{ user.name }</h3>
<button onClick={ handleToggle }>
{ isExpanded ? 'Collapse' : 'Expand' }
</button>
{ isExpanded && (
<div className="user-card__details">
<p>{ user.email }</p>
<button onClick={ () => onSelect( user.id ) }>
Select User
</button>
</div>
) }
</article>
);
};
UserCard.propTypes = {
user: PropTypes.shape( {
id: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
email: PropTypes.string.isRequired,
} ).isRequired,
onSelect: PropTypes.func.isRequired,
};
export default UserCard;WordPress Block Editor
WordPress区块编辑器
When building Gutenberg blocks:
- Use for React (it re-exports React)
@wordpress/element - Use for UI primitives
@wordpress/components - Use for block-specific components
@wordpress/block-editor - Follow the block.json schema for block registration
- Use for state management with WordPress stores
@wordpress/data
构建Gutenberg区块时:
- 使用作为React(它会重新导出React)
@wordpress/element - 使用获取UI基础组件
@wordpress/components - 使用获取区块专属组件
@wordpress/block-editor - 遵循block.json schema进行区块注册
- 使用配合WordPress存储进行状态管理
@wordpress/data