Component
React 组件¶
1. 组件的概念 (Component Concept)¶
定义: 组件是 React 的核心构建块,用于封装 UI 的逻辑和结构。每个组件都是独立的,可复用的模块,可以管理自己的状态和生命周期。
Definition: Components are the core building blocks of React, used to encapsulate the logic and structure of the UI. Each component is an independent, reusable module that can manage its own state and lifecycle.
2. 组件的类型 (Types of Components)¶
函数组件 (Functional Components)¶
定义: 函数组件是最简单的 React 组件,通常用于展示静态内容。它们本质上是接收 props 并返回 React 元素的 JavaScript 函数。
Definition: Functional components are the simplest React components, typically used to display static content. Essentially, they are JavaScript functions that receive props and return React elements.
类组件 (Class Components)¶
定义: 类组件是基于 ES6 类的组件,能够使用状态和生命周期方法。它们适合需要复杂逻辑和状态管理的场景。
Definition: Class components are components based on ES6 classes that can use state and lifecycle methods. They are suitable for scenarios requiring complex logic and state management.
3. 组件的生命周期 (Component Lifecycle)¶
挂载阶段 (Mounting Phase)¶
解释: 组件被创建并插入 DOM 的阶段,包括 constructor
、componentDidMount
方法等。
Explanation: The phase where a component is created and inserted into the DOM, including methods like constructor
and componentDidMount
.
更新阶段 (Updating Phase)¶
解释: 组件的 props 或 state 变化时触发的阶段,包括 componentDidUpdate
方法等。
Explanation: The phase triggered when a component's props or state changes, including methods like componentDidUpdate
.
卸载阶段 (Unmounting Phase)¶
解释: 组件从 DOM 中移除的阶段,包括 componentWillUnmount
方法。
Explanation: The phase where a component is removed from the DOM, including the componentWillUnmount
method.
4. 组件的状态和属性 (State and Props in Components)¶
属性 (Props)¶
定义: Props 是父组件传递给子组件的数据,是不可变的。它们用于在组件之间传递数据和事件处理函数。
Definition: Props are the data passed from a parent component to a child component. They are immutable and are used to pass data and event handlers between components.
状态 (State)¶
定义: State 是组件内部管理的可变数据。State 的变化会触发组件的重新渲染。
Definition: State is mutable data managed within a component. Changes in state trigger a re-render of the component.
5. 组件的复用 (Component Reusability)¶
高阶组件 (Higher-Order Components, HOC)¶
定义: HOC 是一个接收组件并返回新组件的函数,用于复用组件逻辑。
Definition: HOC is a function that takes a component and returns a new component, used to reuse component logic.
Render Props¶
定义: 通过将函数作为 prop 传递给组件,允许复用组件的渲染逻辑。
Definition: A technique where a function is passed as a prop to a component, allowing for the reuse of rendering logic.
6. 组件之间的通信 (Communication Between Components)¶
父子组件通信 (Parent-Child Communication)¶
解释: 通过 props,父组件可以向子组件传递数据,子组件可以通过回调函数向父组件传递数据。
Explanation: Through props, a parent component can pass data to a child component, and the child component can pass data back to the parent component using callback functions.
兄弟组件通信 (Sibling Communication)¶
解释: 通常通过共同的父组件管理状态,并通过 props 进行状态传递实现兄弟组件之间的通信。
Explanation: Sibling communication is typically managed by a shared parent component that holds the state, with props used to pass that state between sibling components.
全局状态管理 (Global State Management)¶
解释: 使用 Context API 或 Redux 等工具,可以在整个应用中共享状态,避免逐层传递 props。
Explanation: Tools like Context API or Redux allow sharing state across the entire application, avoiding the need to pass props through each component level.