function Welcome(props) {
return Hello, {props.name}
;
}
**特点**:
* 简洁:纯函数,没有生命周期方法和 this
* 无状态(Hooks 之前):只能接收 props,不能管理自己的 state
* 性能:没有实例化开销,渲染更快
**使用箭头函数**:
const Welcome = (props) => {
return Hello, {props.name}
;
};
// 简写形式(隐式返回)
const Welcome = (props) => Hello, {props.name}
;
// 解构 props
const Welcome = ({ name }) => Hello, {name}
;
===== 5.2 类组件 =====
类组件使用 ES6 的 class 语法定义,继承自 React.Component。
import React from 'react';
class Welcome extends React.Component {
render() {
return Hello, {this.props.name}
;
}
}
**特点**:
* 可以使用 state 管理组件内部状态
* 可以使用生命周期方法
* 有 this 关键字
* 需要 render() 方法返回 JSX
===== 5.3 组件组合 =====
组件可以相互组合,形成组件树。
function Welcome(props) {
return Hello, {props.name}
;
}
function App() {
return (
);
}
**提取组件**:
当 UI 的一部分被多次使用,或者自身足够复杂时,可以将其提取为独立的组件。
// 原始代码
function Comment(props) {
return (
{props.author.name}
{props.text}
{formatDate(props.date)}
);
}
// 提取 Avatar 组件
function Avatar(props) {
return (
);
}
// 提取 UserInfo 组件
function UserInfo(props) {
return (
{props.user.name}
);
}
// 重构后的 Comment 组件
function Comment(props) {
return (
{props.text}
{formatDate(props.date)}
);
}
===== 5.4 Props 详解 =====
Props(properties 的缩写)是组件之间传递数据的方式。
**传递 props**:
function App() {
return ;
}
**接收 props**:
function User(props) {
return (
姓名:{props.name}
年龄:{props.age}
{props.isAdmin ? '管理员' : '普通用户'}
);
}
// 使用解构
function User({ name, age, isAdmin }) {
return (
姓名:{name}
年龄:{age}
{isAdmin ? '管理员' : '普通用户'}
);
}
**Props 的只读性**:
组件不能修改自己的 props。props 对于组件来说是只读的。
// 错误!props 是只读的
function Welcome(props) {
props.name = 'Hello'; // 错误!
return {props.name}
;
}
===== 5.5 默认 Props =====
为 props 设置默认值:
**函数组件**:
function Button({ text = '点击', type = 'button' }) {
return ;
}
// 或者
function Button(props) {
const { text = '点击', type = 'button' } = props;
return ;
}
**类组件**:
class Button extends React.Component {
static defaultProps = {
text: '点击',
type: 'button'
};
render() {
return ;
}
}
// 或者在组件外部
Button.defaultProps = {
text: '点击',
type: 'button'
};
===== 5.6 PropTypes 类型检查 =====
PropTypes 用于运行时类型检查。
安装:
npm install prop-types
使用:
import PropTypes from 'prop-types';
function User({ name, age, email }) {
return (
{name}
{age}
{email}
);
}
User.propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number,
email: PropTypes.string,
isAdmin: PropTypes.bool,
hobbies: PropTypes.array,
address: PropTypes.object,
onClick: PropTypes.func,
element: PropTypes.element,
nodes: PropTypes.node,
user: PropTypes.instanceOf(User),
shape: PropTypes.shape({
color: PropTypes.string,
fontSize: PropTypes.number
}),
oneOf: PropTypes.oneOf(['News', 'Photos']),
oneOfType: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
arrayOf: PropTypes.arrayOf(PropTypes.number),
objectOf: PropTypes.objectOf(PropTypes.number),
any: PropTypes.any
};
User.defaultProps = {
age: 18
};
**TypeScript 替代方案**:
如果使用 TypeScript,可以使用接口定义 props 类型,获得编译时类型检查。
===== 5.7 children prop =====
children 是一个特殊的 prop,用于在组件标签之间传递内容。
function Card({ title, children }) {
return (
{title}
{children}
);
}
// 使用
function App() {
return (
这是卡片的内容
);
}
**children 的类型**:
* React 元素
* 字符串或数字
* 数组(Fragment)
* 函数(render props)
* null、undefined、boolean(不渲染)
===== 5.8 render props 模式 =====
render props 是一种在 React 组件之间使用一个值为函数的 prop 共享代码的技术。
class MouseTracker extends React.Component {
state = { x: 0, y: 0 };
handleMouseMove = (event) => {
this.setState({
x: event.clientX,
y: event.clientY
});
};
render() {
return (
{this.props.render(this.state)}
);
}
}
// 使用
function App() {
return (
(
鼠标位置:({x}, {y})
)} />
);
}
===== 5.9 高阶组件(HOC) =====
高阶组件是一个函数,接收一个组件并返回一个新的组件。
function withLogger(WrappedComponent) {
return class extends React.Component {
componentDidMount() {
console.log('Component mounted:', WrappedComponent.name);
}
render() {
return ;
}
};
}
// 使用
const EnhancedComponent = withLogger(MyComponent);
**常见的 HOC 用途**:
* 权限控制
* 日志记录
* 数据获取
* 样式增强
===== 5.10 组件的组织方式 =====
**按功能组织**: