Scheduling in React

Scheduling in React

React is a popular JavaScript library for building user interfaces, and it's known for its fast rendering performance. One of the key features that enables this performance is the scheduling mechanism that React uses under the hood.

What is scheduling?

Scheduling is the process of determining when and how often a task should be executed. In the context of React, scheduling refers to the way that React decides when to render components and update the DOM.

In traditional web development, updates to the DOM are typically done synchronously, meaning that they happen immediately when some state changes. However, this approach can lead to poor performance, especially when there are many updates happening at once.

React takes a different approach. Instead of updating the DOM immediately, it schedules updates to happen at a later time. This allows React to batch multiple updates together and perform them all at once, which can greatly improve performance.

How does scheduling work in React?

React's scheduling mechanism is based on two main concepts:

  1. The event loop: The browser has an event loop that manages tasks and events. When a task is added to the event loop, it will be executed as soon as possible, but only when the browser is idle.

  2. Priority levels: Each task in the event loop has a priority level. Higher-priority tasks will be executed before lower-priority tasks.

React makes use of both of these concepts to schedule updates to components. When you call setState or update a prop, React doesn't immediately update the component. Instead, it creates a new update object and adds it to a queue. Then, at some point in the future, React will flush the queue and apply all the updates.

The timing of when React flushes the queue depends on the priority level of the updates. React has several priority levels, including:

  • Immediate: Updates with this priority will be flushed synchronously, meaning that they will be applied immediately.

  • User blocking: Updates with this priority will be flushed before the next paint, meaning that they will be applied before the browser paints the next frame. These updates are typically used for interactions that need to have low latency, such as responding to user input.

  • Normal: Updates with this priority will be flushed after the next paint, meaning that they will be applied in the next frame. These updates are typically used for most UI updates.

  • Low: Updates with this priority will be deferred until the browser is idle. These updates are typically used for non-critical updates, such as preloading data or performing analytics.

React also has a special priority level called Concurrent. Updates with this priority will be split into smaller tasks and interleaved with other high-priority tasks, allowing React to perform work while keeping the app responsive. However, using Concurrent mode requires some additional setup and can be more complex to use correctly.

Example code

Here's an example of how scheduling works in React:

------ - -------- - ---- --------

-------- --------- -
  ----- ------- --------- - ------------

  -------- ------------- -
    -------------- - ---
    -------------- - ---
  -

  ------ -
    -----
      --------- -----------
      ------- ----------------------------------------
    ------
  --
-

In this example, we have a Counter component that displays a count and a button. When the button is clicked, we call setCount twice, which increments the count by two.

However, if you run this code, you'll see that the count only increments by one. That's because both calls to setCount are batched together and treated as a single update. If we want to increment the count by two, we need to pass a function to setCount that takes the previous count as an argument:

-------- ------------- -
  ------------------ -- --------- - ---
  ------------------ -- --------- - ---
-

Now when we click the button, the count will increment by two.

Conclusion

Scheduling is a key feature of React that enables it to achieve fast rendering performance. By scheduling updates to components instead of updating the DOM immediately, React can batch multiple updates together and perform them all at once, which can greatly improve performance.

Understanding how scheduling works in React can help you write more efficient and responsive UI code. By using the right priority levels for your updates and taking advantage of Concurrent mode when appropriate, you can create apps that feel fast and smooth even as they handle complex interactions and large amounts of data.

来源:JavaScript中文网 ,转载请联系管理员! 本文地址:https://www.javascriptcn.com/post/57002


猜你喜欢

  • npm 包 eslint-plugin-extra-rules 使用教程

    在开发前端项目的过程中,代码规范是一个非常重要的问题。代码规范可以提高代码的可读性和可维护性,并且可以减少代码重构的工作量。ESLint 是一个非常流行的 JavaScript 代码检查工具,它可以帮...

    6 年前
  • npm 包 eslint-plugin-you-dont-need-lodash-underscore 使用教程

    如果你正在使用 lodash 或者 underscore,那么你可能会想要使用 eslint-plugin-you-dont-need-lodash-underscore 来帮助你检查你的代码中是否存...

    6 年前
  • npm 包 stylelint-formatter-pretty 使用教程

    在前端开发中,我们经常使用 stylelint 进行 CSS 代码风格检查和格式化,而 stylelint-formatter-pretty 是一个能够把 stylelint 输出结果格式化成易于阅读...

    6 年前
  • npm 包 readable-code 使用教程

    在现代的前端开发中,代码的可读性是非常重要的。可读性高的代码能够降低出错的概率,并且也可以提高代码的可维护性。但是,要写出高可读性的代码并不容易。这时候,我们可以使用 npm 包 readable-c...

    6 年前
  • npm 包 babel-plugin-smart-webpack-import 使用教程

    近年来,前端开发中涌现出了各种各样的工具和技术,让前端开发更加高效快捷。其中,babel-plugin-smart-webpack-import 作为一个非常实用的 npm 包,能够让前端开发者在使用...

    6 年前
  • npm 包 babel-plugin-require-context-hook 使用教程

    babel-plugin-require-context-hook 是一个能帮助前端开发者进行单元测试的 npm 包。它可以在测试期间使用 require.context 来解决导入文件的问题,并且可...

    6 年前
  • npm 包 eslint-config-metalab 使用教程

    简介 eslint-config-metalab 是一款常用的 ESLint 规则库,它是由 MetaLab 团队所维护。ESLint 是一款开源的 JavaScript 代码检测工具,它可以让我们在...

    6 年前
  • NPM 包 babel-literal-to-ast 使用教程

    在前端开发中,我们通常会使用 Babel 这样的工具来将我们编写的代码转换为浏览器可执行的代码。在这个过程中,经常需要将字面量(Literal)转换为 AST(抽象语法树)节点,以便进行更加灵活的操作...

    6 年前
  • npm 包 hsu-scripts 使用教程

    在前端开发中,使用 npm 包可以让我们更加方便的管理和使用各种库和工具。hsu-scripts 是一个常用的 npm 包,它可以帮助我们快速初始化前端项目,并提供一些方便的功能。

    6 年前
  • npm 包 graphql.macro 使用教程

    GraphQL是一种用于API的查询语言,它使得客户端能够精确指定想要的数据,从而减少不必要的数据查询。graphql.macro是一种npm包,它利用了Babel宏的强大功能,使得我们可以在Java...

    6 年前
  • npm 包 babel-plugin-preval 使用教程

    前言 在前端开发中,我们经常需要使用一些预处理工具,如 babel 对 ECMAScript6+ 语法的转换、less 或 sass 等对 css 的预处理等等。这些预处理工具能够增加我们的开发效率并...

    6 年前
  • npm 包 babel-preset-edge 使用教程

    在前端开发中,为了支持更多的浏览器和环境,我们需要使用 babel 进行代码转换。babel 是一个非常流行的 JavaScript 转换工具,它能够将 ES6、ES7 或者 JSX 语法转换成浏览器...

    6 年前
  • npm 包 xxhash 使用教程

    在前端开发中,我们经常会遇到需要对数据进行哈希或校验等操作的情况,这时候可以使用 npm 包 xxhash 来帮助我们完成这些操作。本文将详细介绍 xxhash 的使用方法,并提供示例代码,帮助读者更...

    6 年前
  • NPM 包 Metrohash 使用教程

    介绍 Metrohash 是一个快速、高效的哈希函数,它的特点是在对于小数据块的哈希性能特别优秀。同时,它也是一个支持多种语言的哈希函数库。 在前端开发中,我们常常需要使用哈希算法来实现数据的校验、唯...

    6 年前
  • npm 包 asset-hash 使用教程

    前端开发者常常需要在项目中引入各种资源文件,比如 CSS、JavaScript 和图片等。如果每次更新这些资源文件时都没有一个明确的版本号,会给开发过程带来诸多不便。

    6 年前
  • npm包 postcss-smart-asset 使用教程

    近年来,前端界随着新技术的不断涌现,npm包在前端开发中也逐渐变得不可或缺。npm是Node.js的包管理器,可以搜索、安装、升级和删除包,使得开发人员可以更方便地共享并重复使用代码文件。

    6 年前
  • npm 包 rollup-plugin-rebase 使用教程

    背景 在前端开发中,我们经常需要将多个 JavaScript 文件合并成一个。此时,我们通常会使用类似于 webpack 或者 Rollup 这样的打包工具来完成。

    6 年前
  • npm 包 tosource 使用教程

    npm 包 tosource 是一个能够将 JavaScript 数据结构转换成字符串形式代码的工具。它可以帮助前端开发人员在处理 JavaScript 数据时更加方便快捷地进行操作。

    6 年前
  • npm 包 rollup-plugin-run 使用教程

    在前端开发过程中,我们经常需要将多个 JavaScript 模块打包成单个文件,以便在生产环境中使用。Rollup 是一个 JavaScript 模块打包器,与类似于 Webpack 的工具相比,Ro...

    6 年前
  • npm 包 preppy 使用教程

    preppy 是一个基于 React 的组件库,提供了一系列的 UI 组件和样式。它非常适用于开发快速原型和小型应用。 本文将向您介绍如何使用 preppy,包括安装,配置和具体应用。

    6 年前

相关推荐

    暂无文章