Jacky's blog Jacky's blog
首页
  • 编码专题
  • 深入浅出 Vite
  • 深入浅出 babel
  • 快速上手API
  • 深入浅出 react
  • Node

    • code-notebook
  • 状态管理

    • redux
  • 前端工程化

    • Wepack
  • React源码

    • React源码
  • 组件库封装

    • 组件库
  • 开发工具

    • Vscode 插件
  • 项目展示
  • 案例中心 (opens new window)
  • First Project
  • 基础算法题
  • 链表题
  • 动态规划
  • 双指针
  • 递归
  • 数据结构
  • 前端学习计划 (opens new window)
  • 技术随笔
  • 转载文章
  • 包管理工具
  • 前端学习周报
  • VSCode插件
  • Promise 专题
  • 函数技巧
  • React 专题
  • 配置文件

    • TSCONFIG-配置 (opens new window)
    • NGINX-配置 (opens new window)
    • 正则规则查询手册 (opens new window)
    • Lint 配置 (opens new window)
  • 教程

    • GIT-教程
    • NPM SCRIPTS-工作流 (opens new window)
    • DOCKER-教程 (opens new window)
    • LERNA-教程 (opens new window)
    • GIT-常用操作整理 (opens new window)
  • VSCode

    • LAUNCH.JSON (opens new window)
  • 指令

    • NPM 指令 (opens new window)
    • NVM 指令 (opens new window)
    • Nginx 指令 (opens new window)
    • YARN 指令 (opens new window)
    • PNPM 指令 (opens new window)
  • 库

    • FS-EXTRA 库 (opens new window)
    • NODE 库-PATH (opens new window)
  • 永远的神

    • 魔法师卡颂-自顶向下学 React 源码 (opens new window)
    • 全栈潇晨 (opens new window)
    • 博客-程序员山月-Daily (opens new window)
    • 淘系前端:冴羽 (opens new window)
  • 系列文章

    • 《图解HTTP》 (opens new window)
    • 《ES6标准入门》 (opens new window)
    • 《现代JavaScript教程》 (opens new window)
    • 《深入浅出Webpack》 (opens new window)
    • VSCode 插件系列:小茗同学 (opens new window)
    • JEST 教程 (opens new window)
    • 前端精读周刊:各种精读系列 (opens new window)
    • 一文吃透系列 (opens new window)
    • 图解 REACT 原理 (opens new window)
  • 实用网站

    • MDN (opens new window)
    • CAN I USE (opens new window)
    • TYPESCRIPT-ESLint-RULES (opens new window)
    • ESLint-RULES (opens new window)
    • FRONT-END TREND (opens new window)
    • NPM TREND (opens new window)
    • 在线分析 Node 依赖 (opens new window)
    • FIND NPM (opens new window)
    • CODE PEN (opens new window)
    • 印记中文 (opens new window)
    • TOOL.LU (opens new window)
    • 阮一峰-网道 (opens new window)
    • DIGITAL OCEAN (opens new window)
    • DEVDOCS.IO (opens new window)
    • JOI (opens new window)
  • 算法

    • 小浩算法 (opens new window)
    • LABULADONG 的算法小抄 (opens new window)
    • 力扣 SOLUTION (opens new window)
    • HACKER RANK (opens new window)
    • 代码随想录 (opens new window)
  • 博客系列

    • 美团大佬 (opens new window)
    • 蜡笔小伟 (opens new window)
    • 优秀博客1 (opens new window)
    • 优秀博客2-umi (opens new window)
    • 优质博客 (opens new window)
  • CSS

    • CSS-EASING 库 (opens new window)
    • ROUGH.JS (opens new window)
    • CSS 网站收集
    • UNOCSS (opens new window)
  • 前端

    • PROMISE (opens new window)
    • UNDERSCORE.JS (opens new window)
    • study with BGM (opens new window)
    • nginx【B站视频】 (opens new window)
    • 机器学习
    • Js基础
  • 掘金已购课程

    • 前端自动化测试精讲 (opens new window)
    • 深入浅出 Vite (opens new window)
    • 现代 Web 布局 (opens new window)
    • 前端算法与数据结构 (opens new window)
    • 基于 Vite 的 SSG 框架开发实战 (opens new window)
    • SSR 实战:官网开发指南 (opens new window)
    • WebGL 入门与实践 (opens new window)
    • 玩转 CSS 的艺术之美 (opens new window)
    • 前端调试通关秘籍 (opens new window)
    • React 进阶实践指南 (opens new window)
    • TypeScript 全面进阶指南 (opens new window)
    • 前端缓存技术与方案解析 (opens new window)
    • npm scripts 前端工作流 (opens new window)
    • Webpack5 核心原理与应用实践 (opens new window)
  • 购物车

    • 张鑫旭-技术写作指南 (opens new window)
    • 深入剖析 Node.js 底层原理 (opens new window)
    • 前端开发者的现代 C++ 课 (opens new window)
    • 从前端到全栈 (opens new window)
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Jacky Wang

行到水穷处,坐看云起时
首页
  • 编码专题
  • 深入浅出 Vite
  • 深入浅出 babel
  • 快速上手API
  • 深入浅出 react
  • Node

    • code-notebook
  • 状态管理

    • redux
  • 前端工程化

    • Wepack
  • React源码

    • React源码
  • 组件库封装

    • 组件库
  • 开发工具

    • Vscode 插件
  • 项目展示
  • 案例中心 (opens new window)
  • First Project
  • 基础算法题
  • 链表题
  • 动态规划
  • 双指针
  • 递归
  • 数据结构
  • 前端学习计划 (opens new window)
  • 技术随笔
  • 转载文章
  • 包管理工具
  • 前端学习周报
  • VSCode插件
  • Promise 专题
  • 函数技巧
  • React 专题
  • 配置文件

    • TSCONFIG-配置 (opens new window)
    • NGINX-配置 (opens new window)
    • 正则规则查询手册 (opens new window)
    • Lint 配置 (opens new window)
  • 教程

    • GIT-教程
    • NPM SCRIPTS-工作流 (opens new window)
    • DOCKER-教程 (opens new window)
    • LERNA-教程 (opens new window)
    • GIT-常用操作整理 (opens new window)
  • VSCode

    • LAUNCH.JSON (opens new window)
  • 指令

    • NPM 指令 (opens new window)
    • NVM 指令 (opens new window)
    • Nginx 指令 (opens new window)
    • YARN 指令 (opens new window)
    • PNPM 指令 (opens new window)
  • 库

    • FS-EXTRA 库 (opens new window)
    • NODE 库-PATH (opens new window)
  • 永远的神

    • 魔法师卡颂-自顶向下学 React 源码 (opens new window)
    • 全栈潇晨 (opens new window)
    • 博客-程序员山月-Daily (opens new window)
    • 淘系前端:冴羽 (opens new window)
  • 系列文章

    • 《图解HTTP》 (opens new window)
    • 《ES6标准入门》 (opens new window)
    • 《现代JavaScript教程》 (opens new window)
    • 《深入浅出Webpack》 (opens new window)
    • VSCode 插件系列:小茗同学 (opens new window)
    • JEST 教程 (opens new window)
    • 前端精读周刊:各种精读系列 (opens new window)
    • 一文吃透系列 (opens new window)
    • 图解 REACT 原理 (opens new window)
  • 实用网站

    • MDN (opens new window)
    • CAN I USE (opens new window)
    • TYPESCRIPT-ESLint-RULES (opens new window)
    • ESLint-RULES (opens new window)
    • FRONT-END TREND (opens new window)
    • NPM TREND (opens new window)
    • 在线分析 Node 依赖 (opens new window)
    • FIND NPM (opens new window)
    • CODE PEN (opens new window)
    • 印记中文 (opens new window)
    • TOOL.LU (opens new window)
    • 阮一峰-网道 (opens new window)
    • DIGITAL OCEAN (opens new window)
    • DEVDOCS.IO (opens new window)
    • JOI (opens new window)
  • 算法

    • 小浩算法 (opens new window)
    • LABULADONG 的算法小抄 (opens new window)
    • 力扣 SOLUTION (opens new window)
    • HACKER RANK (opens new window)
    • 代码随想录 (opens new window)
  • 博客系列

    • 美团大佬 (opens new window)
    • 蜡笔小伟 (opens new window)
    • 优秀博客1 (opens new window)
    • 优秀博客2-umi (opens new window)
    • 优质博客 (opens new window)
  • CSS

    • CSS-EASING 库 (opens new window)
    • ROUGH.JS (opens new window)
    • CSS 网站收集
    • UNOCSS (opens new window)
  • 前端

    • PROMISE (opens new window)
    • UNDERSCORE.JS (opens new window)
    • study with BGM (opens new window)
    • nginx【B站视频】 (opens new window)
    • 机器学习
    • Js基础
  • 掘金已购课程

    • 前端自动化测试精讲 (opens new window)
    • 深入浅出 Vite (opens new window)
    • 现代 Web 布局 (opens new window)
    • 前端算法与数据结构 (opens new window)
    • 基于 Vite 的 SSG 框架开发实战 (opens new window)
    • SSR 实战:官网开发指南 (opens new window)
    • WebGL 入门与实践 (opens new window)
    • 玩转 CSS 的艺术之美 (opens new window)
    • 前端调试通关秘籍 (opens new window)
    • React 进阶实践指南 (opens new window)
    • TypeScript 全面进阶指南 (opens new window)
    • 前端缓存技术与方案解析 (opens new window)
    • npm scripts 前端工作流 (opens new window)
    • Webpack5 核心原理与应用实践 (opens new window)
  • 购物车

    • 张鑫旭-技术写作指南 (opens new window)
    • 深入剖析 Node.js 底层原理 (opens new window)
    • 前端开发者的现代 C++ 课 (opens new window)
    • 从前端到全栈 (opens new window)
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 常用资源导航
  • 概念
    • 概念
  • 代码格式化
  • 面向对象基础
  • 构造函数与原型
  • 函数进阶
  • 函数柯里化
  • 函数-传参
  • axios
  • Node 和 ES6 模块系统
  • JS
wangjiasheng
2021-08-02
目录

概念

# 概念

  • React 把组件看成是一个状态机(State Machines)。通过与用户的交互,实现不同状态,然后渲染 UI,让用户界面和数据保持一致。

    React 里,只需更新组件的 state,然后根据新的 state 重新渲染用户界面(不要操作 DOM)。

  • 挂载和卸载

    当组件第一次加载到 DOM 中的时候,这在 React 中被称为挂载。

    同样,每当组件生成的这个 DOM 被移除的时候,这在 React 中被称为卸载。

  • 组件各自隔离

    function App() {
      return (
        <div>
          <Clock />
          <Clock />
          <Clock />
        </div>
      );
    }
    // 每个 Clock 组件都建立了自己的定时器并且独立更新。
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
  • 数据传递

    • 有两种方法props和state

      props用于传递不可变数据

      state可以根据与用户交互来改变

    • 写法

      • 函数组件统一使用形参props接受数据

        点击查看
        function HelloMessage(props) {
            return <h1>Hello {props.name}!</h1>;
        }
         
        const element = <HelloMessage name="Runoob"/>;
         
        ReactDOM.render(
            element,
            document.getElementById('example')
        );
        
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
      • 可以设置默认属性defaultProps

        点击查看
        class HelloMessage extends React.Component {
          render() {
            return (
              <h1>Hello, {this.props.name}</h1>
            );
          }
        }
         
        HelloMessage.defaultProps = {
          name: 'Runoob'
        };
         
        const element = <HelloMessage/>;
         
        ReactDOM.render(
          element,
          document.getElementById('example')
        );
        
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
      • 在父组件中使用state,子组件中接受props

        点击查看
        class WebSite extends React.Component {
          constructor() {
              super();
         
              this.state = {
                name: "菜鸟教程",
                site: "https://www.runoob.com"
              }
            }
          render() {
            return (
              <div>
                <Name name={this.state.name} />
                <Link site={this.state.site} />
              </div>
            );
          }
        }
         
         
         
        class Name extends React.Component {
          render() {
            return (
              <h1>{this.props.name}</h1>
            );
          }
        }
         
        class Link extends React.Component {
          render() {
            return (
              <a href={this.props.site}>
                {this.props.site}
              </a>
            );
          }
        }
         
        ReactDOM.render(
          <WebSite />,
          document.getElementById('example')
        );
        
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
  • React事件处理

    • 驼峰写法

      点击查看

      最容易写错的就是onClick

      <button onClick={activateLasers}>
        激活按钮
      </button>
      
      1
      2
      3
    • 阻止默认行为:e.preventDefault();

    • 如果要用ES6类来写的话需要注意

      1. 在构造函数中,使用this.state创建必要的变量环境

      2. 在构造函数中,使用this.handleClick = this.handleClick.bind(this)修改指针

        因为 <button onClick={this.handleClick}>触发时,this指向button对象,而非定义的ES6环境,如果没有这句,this的值是undefined。

      3. 替代bind的做法

        • 属性初始化器来正确的绑定回调函数:

          class LoggingButton extends React.Component {
            // 这个语法确保了 `this` 绑定在  handleClick 中
            // 这里只是一个测试
            handleClick = () => {
              console.log('this is:', this);
            }
            render() {
              return (
                <button onClick={this.handleClick}>
                  Click me
                </button>
              );
            }
          }
          
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
        • 传递回调函数

           render() {
              //  这个语法确保了 `this` 绑定在  handleClick 中
              return (
                <button onClick={(e) => this.handleClick(e)}>
                  Click me
                </button>
              );
          
          1
          2
          3
          4
          5
          6
          7

          使用这个语法有个问题就是每次 LoggingButton 渲染的时候都会创建一个不同的回调函数。在大多数情况下,这没有问题。然而如果这个回调函数作为一个属性值传入低阶组件,这些组件可能会进行额外的重新渲染。我们通常建议在构造函数中绑定或使用属性初始化器语法来避免这类性能问题。

        • 向事件中传参

          // 替换bind方法2: 使用回调函数
          <button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
          // bind方法
          <button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>
          
          1
          2
          3
          4
  • 条件渲染

    • React 中的条件渲染和 JavaScript 中的一致,使用 JavaScript 操作符 if 或条件运算符来创建表示当前状态的元素

        if (isLoggedIn) {
          return <UserGreeting />;
        }
        return <GuestGreeting />;
      
      1
      2
      3
      4
    • 使用元素进行驱动,核心使用this.setState

      ...
        handleLoginClick() {
          this.setState({isLoggedIn: true});
        }
       
        handleLogoutClick() {
          this.setState({isLoggedIn: false});
        }
       
        render() {
          const isLoggedIn = this.state.isLoggedIn;
       
          let button = null;
          if (isLoggedIn) {
            button = <LogoutButton onClick={this.handleLogoutClick} />;
          } else {
            button = <LoginButton onClick={this.handleLoginClick} />;
          }
      ...
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
    • 技巧:使用&&

      关键代码:

      <h1>Hello!</h1>
      {unreadMessages.length > 0 &&
          <h2>
          您有 {unreadMessages.length} 条未读信息。
              </h2>
      }
      // 在 JavaScript 中,true && expression 总是返回 expression,而 false && expression 总是返回 false。
      
      // 因此,如果条件是 true,&& 右侧的元素就会被渲染,如果是 false,React 会忽略并跳过它。
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
    • 技巧:三元表达式condition ? true : false。

  • 快速创建列表

    • map 方法,前提必须要给一个key

        const listItems = numbers.map((number) =>
          <li key={number.toString()}>
            {number}
          </li>
        );
        return (
          <ul>{listItems}</ul>
        );
      
      1
      2
      3
      4
      5
      6
      7
      8
    • key最好使用数据的id,在数据没有id时,使用序列号索引

      const todoItems = todos.map((todo, index) =>
        // 只有在没有确定的 id 时使用
        <li key={index}>
          {todo.text}
        </li>
      );
      
      1
      2
      3
      4
      5
      6
    • key的位置,应该在同级的<li>或者同级的<ListItem>

  • React API

    • setState:setState(object nextState[, function callback])

      • 不能在组件内部通过this.state修改状态,因为该状态会在调用setState()后被替换。
      • setState()并不会立即改变this.state,而是创建一个即将处理的state
      • setState()并不一定是同步的,为了提升性能React会批量执行state和DOM渲染。
      • setState()总是会触发一次组件重绘,除非在shouldComponentUpdate()中实现了一些条件渲染逻辑。
    • replaceState:replaceState(object nextState[, function callback])

      • callback,可选参数,回调函数。该函数会在replaceState设置成功,且组件重新渲染后调用
    • setProps

      • props相当于组件的数据流,它总是会从父组件向下传递至所有的子组件中。
      • 向组件传递数据
      • 通知React.render()重新渲染
      • 更新组件的方式:1. React.render()2. setProps
    • replaceProps

      • replaceProps()方法与setProps类似,但它会删除原有 props。
    • isMounted

      • 用于判断组件是否已挂载到DOM中。可以使用该方法保证了**setState()和forceUpdate()**在异步场景下的调用不会出错
  • React 组件生命周期

    • Mounting:修改DOM/ Unmounting:已移出真实 DOM
    • Updating:重新渲染
    • 具体的方法
      • DOM相关
        • componentWillMount
        • componentDidMount
        • componentWillUnmount
      • 数据相关
        • componentWillReceiveProps
        • shouldComponentUpdate
  • React AJAX

  • componentDidMount 方法中的 Ajax 来获取

  • R

编辑 (opens new window)
上次更新: 2022/04/06, 15:04:00
常用资源导航
代码格式化

← 常用资源导航 代码格式化→

最近更新
01
如何理解浏览器的 user agent 用户代理的含义?
11-05
02
浏览器事件循环机制
10-31
03
浏览器页面渲染机制【2023】
10-15
更多文章>
Theme by Vdoing | Copyright © 2020-2023
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式