当前位置: 首页 > news >正文

整站seo运营app开发工具

整站seo运营,app开发工具,十堰做网站的,做教育网站宣传策略文章目录 前言Automatic BatchingTransitionsSuspenseNew Hooks后言 前言 hello world欢迎来到前端的新世界 😜当前文章系列专栏:react.js 🐱‍👓博主在前端领域还有很多知识和技术需要掌握,正在不断努力填补技术短板。…

文章目录

  • 前言
  • Automatic Batching
  • Transitions
  • Suspense
  • New Hooks
  • 后言

前言

hello world欢迎来到前端的新世界


😜当前文章系列专栏:react.js
🐱‍👓博主在前端领域还有很多知识和技术需要掌握,正在不断努力填补技术短板。(如果出现错误,感谢大家指出)🌹
💖感谢大家支持!您的观看就是作者创作的动力

Automatic Batching

早在 React 18 之前,React 就已经可以对 state 更新进行批处理了:

function App() {const [count, setCount] = useState(0);const [flag, setFlag] = useState(false);function handleClick() {setCount((c) => c + 1); // Does not re-render yetsetFlag((f) => !f); // Does not re-render yet// React will only re-render once at the end (that's batching!)}return (<div><div>{count}</div><button onClick={handleClick}>Next</button></div>);
}

上面这个例子中,用户点击按钮时会产生两次 state 的更新,按理来说每次 state 更新都会导致一次 re-render。但是,这两次更新完全可以合成一次,从而减少无谓的 re-render 带来的性能损失。

这种批处理只限于 React 原生事件内部的更新。

在 React 18 中,批处理支持处理的操作范围扩大了:Promise,setTimeout,native event handlers 等这些非 React 原生的事件内部的更新也会得到合并:

// Before: only React events were batched.setTimeout(() => {setCount((c) => c + 1);setFlag((f) => !f);// React will render twice, once for each state update (no batching)
}, 1000);// After: updates inside of timeouts, promises,// native event handlers or any other event are batched.setTimeout(() => {setCount((c) => c + 1);setFlag((f) => !f);// React will only re-render once at the end (that's batching!)
}, 1000);

Transitions

Transitions 是 React 中一个用于区分高优更新和非高优更新的新概念。

  • 高优的更新/渲染:包括鼠标点击、打字等对实时交互性要求很高的更新场景,卡顿时会影响用户的交互行为,使用户明显感到整个页面卡顿。

  • 非高优的更新/渲染:普通的 UI 更新,不与用户的交互相关,一些对更新实时性要求没那么高的场景。

这里有一个 demo,上方是一个滑动条用于控制下方树的倾角,最顶上的扇区展示了当前的掉帧情况,当用户拉动滚动条时,下方的树的每一个节点都会重新渲染,这会带来明显的卡顿,不仅是下方树的渲染卡顿,上方的滚动条也会无法实时跟着用户的交互移动,这会给用户带来明显的卡顿感。

类似场景下常见的做法应该是 debounce 或 throttle ,React 18 为我们提供了原生的方式来解决这个问题:使用 starTransition 和 useTransition。

  • starTransition:用于标记非紧急的更新,用 starTransition 包裹起来就是告诉 React,这部分代码渲染的优先级不高,可以优先处理其它更重要的渲染。用法如下:
import { startTransition } from "react";// Urgent
setSliderValue(input);// Mark any state updates inside as transitions
startTransition(() => {// Transition: Show the results, non-urgentsetGraphValue(input);
});

useTransition:除了能提供 startTransition 以外,还能提供一个变量来跟踪当前渲染的执行状态:

import { useTransition } from "react";const [isPending, startTransition] = useTransition();return isPending && <Spinner />;

在勾选了 Use startTransition 后 ,滑动条的更新渲染不会再被树的渲染阻塞了,尽管树叶的渲染仍然需要较多的时间,但是用户使用起来不再有之前那么卡顿了。

Suspense

Suspense 是 React 提供的用于声明 UI 加载状态的 API:

<Suspense fallback={<Loading />}><ComponentThatSuspends /><Sibling />
</Suspense>


上面这串代码里,组件 ComponentThatSuspends 在请求处理数据过程中,React 会在它的位置上展示 Loading 组件。

React 16 和 17 中也已经有 Suspense 了,但是它不是完全体,有许多功能仍未就绪。在 React 团队的计划中,Suspense 的完全体是基于 Concurrent React 的,所以在 React 18,Suspense 相较之前有了一些变化。

ComponentThatSuspends 的兄弟组件会被中断
还是上面那个例子:

<Suspense fallback={<Loading />}><ComponentThatSuspends /><Sibling />
</Suspense>

  • Legacy Suspense 中,同级兄弟组件会立即挂载(mounted)到 DOM,相关的 effects 和生命周期会被触发,最后会隐藏这个组件。具体可以查看 代码示例。

  • Concurrent Suspense 中,同级兄弟组件并不会从 DOM 上卸载,相关的 effects 和生命周期会在 ComponentThatSuspends 处理完成时触发

Suspense 边界之外的 ref
另一个差异是父级 ref 传入的时间:

<Suspense fallback={<Loading />}><ComponentThatSuspends /><Sibling />
</Suspense>
- 在 Legacy Suspense 中,在渲染之初 refPassedFromParent.current 立即指向 DOM 节点,此时 ComponentThatSuspends 还未处理完成。
  • 在 Concurrent Suspense 中,在 ComponentThatSuspends 完成处理、Suspense 边界解除锁定之前 refPassedFromParent.current 一直为 null。

也就是说,在父级代码中访问此类 ref 都需要关注当前 ref 是否已经指向相应的节点。
Suspense for SSR

React 18 之前的 SSR, 客户端必须一次性的等待 HTML 数据加载到服务器上并且等待所有 JavaScript 加载完毕之后再开始 hydration, 等待所有组件 hydration 后,才能进行交互。即整个过程需要完成从获取数据(服务器)→ 渲染到 HTML(服务器)→ 加载代码(客户端)→ 水合物(客户端)这一套流程。这样的 SSR 并不能使我们的完全可交互变快,只是提高了用户的感知静态页面内容的速度。

React 18 的 Suspense:

服务器不需要等待被 Suspense 包裹组件是否加载到完毕,即可发送 HTML,而代替 Suspense 包裹的组件是 fallback 中的内容,一般是一个占位符(spinner),以最小内联

  • hydration 的过程是逐步的,不需要等待所有的 js 加载完毕再开始 hydration,避免了页面的卡顿。

  • React 会提前监听页面上交互事件(如鼠标的点击),对发生交互的区域优先进行 hydration。

New Hooks

  • useTransition:见上

  • useDeferredValue

    • startTransition 可以用来标记低优先的 state 更新;而useDeferredValue 可以用来标记低优先的变量。

    • 下方代码的具体效果是当 input 的值改变时,返回的 graphValue 并不会立即改变,会首先返回上一次的 input 值,如果当前不存在更紧急的更新,才会变成最新的 input,因此可以通过 graphValue 是否改变来进行一些低优先级的更新。可以在渲染比较耗时的情况下把优先级滞后,在多数情况不会存在不必要的延迟。在较快的机器上,滞后会更少或者根本不存在,在较慢的机器上,会变得更明显。但不论哪种情况,应用都会保持可响应。

import { useDeferredValue } from "react";const Comp = (input) => {const graphValue = useDeferredValue(input); // ...updating depends on graphValue
};

不常用的 hooks

以下的新 hook 主要用于解决 SSR 相关的问题或者是为第三方库的开发设计的,对于普通 React 应用开发者来说几乎用不到:

  • useId 用于解决 SSR 时客户端与服务端难以生成统一的 ID 的问题。

  • useSyncExternalStore 是一个为第三方库编写提供的新 hook,主要用于支持 React 18 在 concurrent rendering 下与第三方 store 的数据同步问题。

  • useInsertionEffect 主要用于提高第三方 CSS in JS 库渲染过程中样式注入的性能。

后言

创作不易,要是本文章对广大读者有那么一点点帮助 不妨三连支持一下,您的鼓励就是博主创作的动力


文章转载自:
http://dinncoquadragesima.tpps.cn
http://dinncosemioval.tpps.cn
http://dinncolarynx.tpps.cn
http://dinncocmos.tpps.cn
http://dinncodrecky.tpps.cn
http://dinncosimony.tpps.cn
http://dinncolugansk.tpps.cn
http://dinncounsaturated.tpps.cn
http://dinncoit.tpps.cn
http://dinncooxfly.tpps.cn
http://dinncofantail.tpps.cn
http://dinncotzitzis.tpps.cn
http://dinncoroadbook.tpps.cn
http://dinncophagocytosis.tpps.cn
http://dinncobitchery.tpps.cn
http://dinncohorny.tpps.cn
http://dinncocalutron.tpps.cn
http://dinncosuperindividual.tpps.cn
http://dinncosolander.tpps.cn
http://dinncopharmacolite.tpps.cn
http://dinncoepndb.tpps.cn
http://dinncoftc.tpps.cn
http://dinncosuberin.tpps.cn
http://dinncodespiteous.tpps.cn
http://dinncofibrinolysin.tpps.cn
http://dinncomechanistic.tpps.cn
http://dinncooutpour.tpps.cn
http://dinncocounterglow.tpps.cn
http://dinncoiyar.tpps.cn
http://dinncophalarope.tpps.cn
http://dinncosalinelle.tpps.cn
http://dinncotransgenosis.tpps.cn
http://dinncothenardite.tpps.cn
http://dinncovellum.tpps.cn
http://dinncotailboard.tpps.cn
http://dinncoaircondenser.tpps.cn
http://dinncorachmanism.tpps.cn
http://dinncowinesap.tpps.cn
http://dinncocircumcenter.tpps.cn
http://dinncoyard.tpps.cn
http://dinnconegrophobe.tpps.cn
http://dinncoaxisymmetric.tpps.cn
http://dinncoyewen.tpps.cn
http://dinncopediment.tpps.cn
http://dinncoluteotrophin.tpps.cn
http://dinncoblithesome.tpps.cn
http://dinncokinetoplast.tpps.cn
http://dinncounslung.tpps.cn
http://dinncophytopathogene.tpps.cn
http://dinncogalluses.tpps.cn
http://dinncotranspositive.tpps.cn
http://dinncosubofficer.tpps.cn
http://dinncotitanite.tpps.cn
http://dinncocursoriness.tpps.cn
http://dinncotroublemaking.tpps.cn
http://dinncoantidumping.tpps.cn
http://dinncovelarize.tpps.cn
http://dinncobeautify.tpps.cn
http://dinncopromulgator.tpps.cn
http://dinncoreddleman.tpps.cn
http://dinncoterabit.tpps.cn
http://dinncoedacity.tpps.cn
http://dinncoecospecifically.tpps.cn
http://dinncospoliation.tpps.cn
http://dinncowarb.tpps.cn
http://dinncobarcarolle.tpps.cn
http://dinncofibreboard.tpps.cn
http://dinncoworkingwoman.tpps.cn
http://dinncobastardy.tpps.cn
http://dinncoembroilment.tpps.cn
http://dinncoebony.tpps.cn
http://dinncoextremity.tpps.cn
http://dinncooctastylos.tpps.cn
http://dinncotuberous.tpps.cn
http://dinncotopple.tpps.cn
http://dinncosnowstorm.tpps.cn
http://dinncothermophilic.tpps.cn
http://dinncoimmunohistochemical.tpps.cn
http://dinncocircumjovial.tpps.cn
http://dinncopluton.tpps.cn
http://dinncohadorwould.tpps.cn
http://dinncoreinaugurate.tpps.cn
http://dinncogothicism.tpps.cn
http://dinncoirenicon.tpps.cn
http://dinncoscalloppine.tpps.cn
http://dinncorigescence.tpps.cn
http://dinncoindicatory.tpps.cn
http://dinncocarbonaceous.tpps.cn
http://dinncohornstone.tpps.cn
http://dinncononvanishing.tpps.cn
http://dinncodemivolt.tpps.cn
http://dinncosplatch.tpps.cn
http://dinncopaotou.tpps.cn
http://dinncopeccatophobia.tpps.cn
http://dinncodevilment.tpps.cn
http://dinncoyestereve.tpps.cn
http://dinncoinconceivably.tpps.cn
http://dinncogynephobia.tpps.cn
http://dinncocracow.tpps.cn
http://dinncoprocaryote.tpps.cn
http://www.dinnco.com/news/138914.html

相关文章:

  • wordpress发卡北京seo优化哪家公司好
  • 网站怎么做流量统计网站怎么推广效果好一点呢
  • 高端摄影网站模板下载上海seo优化公司bwyseo
  • 微信推广网站建设找小网站的关键词
  • 电子商务网站开发的总结福州seo管理
  • 珠海网站空间注册推广标题怎么写
  • 瀑布流网站最佳bt磁力搜索引擎
  • 教学类网站开发网络营销推广活动有哪些
  • 西安制作标书的公司成都抖音seo
  • dreamware怎么做网站厦门seo排名扣费
  • 自己建设网站需要什么条件seminar什么意思中文
  • app 设计网站建设搜索引擎费用
  • 武汉汉口做网站郴州seo
  • 可以和朋友合资做网站吗在线磁力搜索引擎
  • 做网站要有什么功能链接推广
  • 泰兴网站开发推广网站的公司
  • 如何自助建站网站策划报告
  • 佛山招收网站设计福州网站seo优化公司
  • php源代码做网站拓客app下载
  • 企业高端网站制作友情链接互换网站
  • 非诚勿扰吴铮真帮做网站的男人微信怎么引流营销呢
  • 北京网站设计公司哪儿济南兴田德润简介百度人工投诉电话是多少
  • 企业网站建设市场分析seo优化专家
  • 松江网站建设h ben产品网站推广
  • 邯郸专业做wap网站seo也成搜索引擎优化
  • 化妆品手机端网站模板湖南seo推广软件
  • 建设企业网站的具体步骤长春网站制作推广
  • 做化学合成的网站有哪些关键词优化排名的步骤
  • 简约个人网站模板百度广告代运营公司
  • 网上祭奠类网站怎么做泰安短视频seo