ReactJS 教程

ReactJS - 主页 ReactJS - 简介 ReactJS - 路线图 ReactJS - 安装 ReactJS - 功能 ReactJS - 优势和缺点 ReactJS - 架构 ReactJS - 创建 React 应用程序 ReactJS - JSX ReactJS - 组件 ReactJS - 嵌套组件 ReactJS - 使用组件 ReactJS - 集合组件 ReactJS - 样式 ReactJS - 属性 (props) ReactJS - 使用属性创建组件 ReactJS - props 验证 ReactJS - 构造函数 ReactJS - 组件生命周期 ReactJS - 事件管理 ReactJS - 创建事件感知组件 ReactJS - Expense Manager 事件 ReactJS - 状态管理 ReactJS - 状态管理 API ReactJS - 无状态组件 ReactJS - Hooks 进行状态管理 ReactJS - Hooks 的组件生命周期 ReactJS - 布局组件 ReactJS - 分页 ReactJS - Material UI ReactJS - Http 客户端编程 ReactJS - 表单编程 ReactJS - 受控组件 ReactJS - 非受控组件 ReactJS - Formik ReactJS - 条件渲染 ReactJS - 列表 ReactJS - Key 键 ReactJS - 路由 ReactJS - Redux ReactJS - 动画 ReactJS - Bootstrap ReactJS - Map ReactJS - 表格 ReactJS - 使用 Flux 管理状态 ReactJS - 测试 ReactJS - CLI 命令 ReactJS - 构建和部署 ReactJS - 示例

Hooks

ReactJS - Hooks 简介 ReactJS - 使用 useState ReactJS - 使用 useEffect ReactJS - 使用 useContext ReactJS - 使用 useRef ReactJS - 使用 useReducer ReactJS - 使用 useCallback ReactJS - 使用 useMemo ReactJS - 自定义 Hooks

ReactJS 高级

ReactJS - 可访问性 ReactJS - 代码拆分 ReactJS - 上下文 ReactJS - 错误边界 ReactJS - 转发 Refs ReactJS - 片段 ReactJS - 高阶组件 ReactJS - 与其他库集成 ReactJS - 优化性能 ReactJS - Profiler API ReactJS - Portals ReactJS - 不使用 ES6 ECMAScript ReactJS - 不使用 JSX 的 React ReactJS - Reconciliation ReactJS - Refs 和 DOM ReactJS - 渲染道具 ReactJS - 静态类型检查 ReactJS - 严格模式 ReactJS - Web 组件

其他概念

ReactJS - 日期选择器 ReactJS - Helmet ReactJS - 内联样式 ReactJS - PropTypes ReactJS - BrowserRouter ReactJS - DOM ReactJS - 轮播 ReactJS - 图标 ReactJS - 表单组件 ReactJS - 参考 API

ReactJS 有用资源

ReactJS - 快速指南 ReactJS - 备忘录 Axios - 备忘录 ReactJS - 有用资源 ReactJS - 讨论


ReactJS - 嵌套组件

React 中的嵌套组件是与另一个组件相关的组件。您也可以将其视为父组件内的子组件;但它们不是使用继承概念链接在一起的,而是使用组合概念。因此,所有组件都嵌套在一起,以创建一个更大的组件,而不是从父组件继承的较小组件。

React 组件是 React 应用程序的构建块。React 组件由多个单独的组件组成。React 允许将多个组件组合在一起以创建更大的组件。此外,React 组件可以嵌套到任意级别。

嵌套组件将使您的代码更高效、更结构化。但是,如果组件嵌套或组装不正确,您的代码可能会变得更加复杂,从而导致效率降低。让我们在本章中了解如何正确组合 React 组件。

FormattedMoney 组件

让我们创建一个组件 FormattedMoney,在渲染之前将金额格式化为小数点后两位。

步骤 1 − 在您最喜欢的编辑器中打开我们的 expense-manager 应用程序。

src/components 文件夹中创建一个名为 FormattedMoney.js 的文件,然后导入 React 库。

import React from 'react';

步骤 2 −然后通过扩展 React.Component 创建一个类 FormattedMoney

class FormattedMoney extends React.Component {
}

接下来,引入带有参数 props 的构造函数,如下所示 −

constructor(props) {
    super(props);
}

创建方法 format() 来格式化金额。

format(amount) {
    return parseFloat(amount).toFixed(2)
}

创建另一个方法 render() 来发出格式化的金额。

render() {
   return (
      <span>{this.format(this.props.value)}</span> 
   ); 
}

在这里,我们通过 this.props 传递 value 属性来使用 format 方法。

步骤 3 − 接下来,将组件指定为默认导出类。

export default FormattedMoney;

现在,我们已经成功创建了 FormattedMoney React 组件。

import React from 'react';

class FormattedMoney extends React.Component {
   constructor(props) {
      super(props)
   }
   format(amount) {
      return parseFloat(amount).toFixed(2)
   }
   render() {
      return (
         <span>{this.format(this.props.value)}</span>
      );
   }
}
export default FormattedMoney;

FormattedDate 组件

让我们创建另一个组件 FormattedDate 来格式化并显示费用的日期和时间。

步骤 1 − 在您最喜欢的编辑器中打开我们的 expense-manager 应用程序。

src/components 文件夹中创建一个文件 FormattedDate.js 并导入 React 库。

import React from 'react';

步骤 2 −接下来,通过扩展 React.Component 创建一个类。

class FormattedDate extends React.Component {
}

然后引入带有参数 props 的构造函数,如下所示 −

constructor(props) {
    super(props);
}

步骤 3 − 接下来,创建一个方法 format() 来格式化日期。

format(val) {
   const months = ["JAN", "FEB", "MAR","APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
   let parsed_date = new Date(Date.parse(val));
   let formatted_date = parsed_date.getDate() + 
      "-" + months[parsed_date.getMonth()] + 
      "-" + parsed_date.getFullYear()
   return formatted_date;
}

创建另一个方法 render() 来发出格式化的日期。

render() { return ( <span>{this.format(this.props.value)}</span> ); }

在这里,我们通过 this.props 传递 value 属性来使用 format 方法。

步骤 4 − 接下来,将组件指定为默认导出类。

export default FormattedDate;

现在,我们已经成功创建了我们的 FormattedDate React 组件。完整代码如下 −

import React from 'react';
class FormattedDate extends React.Component {
   constructor(props) {
      super(props)
   }
   format(val) {
      const months = ["JAN", "FEB", "MAR","APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
      let parsed_date = new Date(Date.parse(val));
      let formatted_date = parsed_date.getDate() + 
         "-" + months[parsed_date.getMonth()] + 
         "-" + parsed_date.getFullYear()
      return formatted_date;
   }
   render() {
      return (
         <span>{this.format(this.props.value)}</span>
      );
   }
}
export default FormattedDate;