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

面包店网站建设论文网络推广seo怎么做

面包店网站建设论文,网络推广seo怎么做,网站建设工具有哪些,酒店网站制作React数据管理 代码仓库 React批量更新 React中的批量更新就是将多次更新合并处理,最终只渲染一次,来获得更好的性能。 React18版本之前的批量更新 // react 17 react-dom 17 react-scripts 4.0.3 import * as ReactDOM from "react-dom"…

React数据管理

代码仓库

React批量更新

React中的批量更新就是将多次更新合并处理,最终只渲染一次,来获得更好的性能。

React18版本之前的批量更新

// react 17 react-dom 17 react-scripts 4.0.3
import * as ReactDOM from "react-dom";
import {useState} from 'react'
function App() {const [num1, setNum1] = useState(0);const [num2, setNum2] = useState(0);function handleClick() {setNum1(c => c + 1);setNum2(c => c + 1);}console.log("render");return (<div>num1:{num1}-num2:{num2}<button onClick={handleClick}>Next</button></div>);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

点击一次按钮触发setNum1和setNum2,但是render只输出一次,React将多次更新合并处理,最终只渲染一次。

然而React18版本之前,批量更新并不是所有场景都会生效。

import * as ReactDOM from "react-dom";
import {useState} from 'react'
function App() {const [num1, setNum1] = useState(0);const [num2, setNum2] = useState(0);function handleClick() {  setTimeout(()=>{setNum1(c => c + 1);setNum2(c => c + 1);})}console.log("render");return (<div>num1:{num1}-num2:{num2}<button onClick={handleClick}>Next</button></div>);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

点击一次按钮触发setNum1和setNum2,然后会输出两次render,这就是在React18版本之前,React的批量更新失效了。

总结:在React18之前,我们只能在React事件处理函数中执行过程中进行批量更新。对于promise、setTimeout、原生事件处理函数或其他任何事件中的状态更新都不会进行批量更新。

React18自动批量更新

从React18的createRoot开始,无论在哪里,所有更新都将自动进行批量更新。
这意味着 setTimeout、promises、原生事件处理函数或其他任何事件的批量更新都将与 React 事件一样,以相同的方式进行批量更新。我们希望这样可以减少渲染工作量,从而提高应用程序的性能:

// react 18.2.0 react-dom 18.2.0 react-scripts 5.0.1
import ReactDOM from 'react-dom/client';
import {useState} from 'react'
function App() {const [num1, setNum1] = useState(0);const [num2, setNum2] = useState(0);function handleClick() {  setTimeout(()=>{setNum1(c => c + 1);setNum2(c => c + 1);})}console.log("render");return (<div>num1:{num1}-num2:{num2}<button onClick={handleClick}>Next</button></div>);
}
const root = ReactDOM.createRoot(document.getElementById('root')
);
root.render(<App />
);

点击一次按钮触发setNum1和setNum2,然后会输出一次render,这就是在React18版本,自动批量更新,所有更新都将进行批量更新。

禁止批量更新

使用flushSync来包裹更新,做到禁止批量更新

import ReactDOM from 'react-dom/client';
import {flushSync} from 'react-dom';
import {useState} from 'react';
function App() {const [num1, setNum1] = useState(0);const [num2, setNum2] = useState(0);function handleClick() {  setTimeout(()=>{flushSync(()=>{setNum1(c => c + 1);})flushSync(()=>{setNum2(c => c + 1);})})}console.log("render");return (<div>num1:{num1}-num2:{num2}<button onClick={handleClick}>Next</button></div>);
}
const root = ReactDOM.createRoot(document.getElementById('root')
);
root.render(<App />
);

React18的自动批量更新对Hooks的影响

如果你正在使用 Hooks,在绝大多数情况下批量更新都能“正常工作”。

React18的自动批量更新对Classes的影响

如果在React17版本之前,没有在React事件处理函数中执行的代码不会批量执行,同时它也是同步执行,而在React18版本中,都是批量更新和异步执行的。所以会导致React18版本和React18版本之前的输出不一样。

在React18版本之前

import React, { Component } from 'react'
import ReactDOM from 'react-dom'
export default class App extends Component {constructor(props) {super(props);this.state = {num1: 1,num2: 1}}handleClick(){setTimeout(()=>{this.setState(({num1})=>({num1: num1+1}))console.log(this.state);this.setState(({num2})=>({num2: num2+1}))})}render() {console.log('render');return (<div>num1:{this.state.num1}-num2:{this.state.num2}<button onClick={()=>{this.handleClick()}}>Next</button></div>)}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
/*上述代码点击的时候会输出一下内容:render{num1: 2, num2: 1}render
*/

在React18版本

import React, { Component } from 'react'
import ReactDOM from 'react-dom/client';
export default class App extends Component {constructor(props) {super(props);this.state = {num1: 1,num2: 1}}handleClick(){setTimeout(()=>{this.setState(({num1})=>({num1: num1+1}))console.log(this.state);this.setState(({num2})=>({num2: num2+1}))})}render() {console.log('render');return (<div>num1:{this.state.num1}-num2:{this.state.num2}<button onClick={()=>{this.handleClick()}}>Next</button></div>)}
}
const root = ReactDOM.createRoot(document.getElementById('root')
);
root.render(<App />
);
/*上述代码点击的时候会输出一下内容:{num1: 1, num2: 1}render
*/

如果想要在React18中按照React18版本之前的输出需要使用flushSync来进行修改

import React, { Component } from 'react'
import { flushSync } from 'react-dom';
import ReactDOM from 'react-dom/client';
export default class App extends Component {constructor(props) {super(props);this.state = {num1: 1,num2: 1}}handleClick(){setTimeout(()=>{flushSync(()=>{this.setState(({num1})=>({num1: num1+1}))})console.log(this.state);this.setState(({num2})=>({num2: num2+1}))})}render() {console.log('render');return (<div>num1:{this.state.num1}-num2:{this.state.num2}<button onClick={()=>{this.handleClick()}}>Next</button></div>)}
}
const root = ReactDOM.createRoot(document.getElementById('root')
);
root.render(<App />
);
/*上述代码点击的时候会输出一下内容:{num1: 1, num2: 1}render
*/

useState是同步还是异步

先上结论:

  1. 在React18版本之前,useState在React合成事件和hooks中是异步的,其他情况都是同步的,例如,原生事件、setTimeout、promise等
  2. 在React18版本,useState在React中都是异步的

React18版本之前的this.setState

import * as ReactDOM from "react-dom";
import React,{useState,useEffect} from 'react'
class App extends React.Component {state = {count: 0}componentDidMount() {this.setState({count: this.state.count + 1})console.log(this.state.count);this.setState({count: this.state.count + 1})console.log(this.state.count);setTimeout(() => {this.setState({count: this.state.count + 1})console.log(this.state.count);this.setState({count: this.state.count + 1})console.log(this.state.count);});}render() {return <h1>Count: {this.state.count}</h1>}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

上述代码输出0 0 2 3,我们来分析一下为什么这样输出。

  1. 首先进入componentDidMount,因为this.setState是异步执行的,所以先执行同步代码,执行两次console.log(this.state.count);,输出两次0
  2. componentDidMount中,this.setState导致的更新会进行合并,批量更新,只会更新最后一个,所以当还没执行setTimeout之前,更新count,这时的count变成了1
  3. 进入setTimeout执行,在setTimeout中,this.setState是同步执行的,并且没有批量更新
  4. 执行this.setState({count: this.state.count + 1}),这时count同步更新为2,执行console.log(this.state.count);输出2
  5. 执行this.setState({count: this.state.count + 1}),这时count同步更新为3,执行console.log(this.state.count);输出4

所以最终输出0 0 2 3

React18版本之前的this.setState怎么在setTimeout实现异步批量更新(unstable_batchedUpdates)

this.setState怎么在setTimeout实现异步批量更新,React提供了一种解决方案,就是从react-dom中暴露一个API:unstable_batchedUpdates,我们看一下具体的用法。

import * as ReactDOM from "react-dom";
import React,{useState,useEffect} from 'react'
class App extends React.Component {state = {count: 0}componentDidMount() {this.setState({count: this.state.count + 1})console.log(this.state.count);this.setState({count: this.state.count + 1})console.log(this.state.count)setTimeout(() => {ReactDOM.unstable_batchedUpdates(() => {this.setState({count: this.state.count + 1})console.log(this.state.count)this.setState({count: this.state.count + 1})console.log(this.state.count)}) })}render() {return <h1>Count: {this.state.count}</h1>}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

上述代码输出0 0 1 1,我们来分析一下为什么这样输出。

  1. 首先进入componentDidMount,因为this.setState是异步执行的,所以先执行同步代码,执行两次console.log(this.state.count);,输出两次0
  2. componentDidMount中,this.setState导致的更新会进行合并,批量更新,只会更新最后一个,所以当还没执行setTimeout之前,更新count,这时的count变成了1
  3. 进入setTimeout执行,因为setTimeout中的代码用了unstable_batchedUpdates嵌套,所以也是异步批量更新。
  4. 先执行同步代码,执行两次console.log(this.state.count),输出两次1。
  5. 执行this.setState({count: this.state.count + 1}),此时count更新为2。
  6. 执行this.setState({count: this.state.count + 1}),此时count更新为3。

React18的解决方案

如果在React17版本中,想要在setTimeout中执行异步批量更新,需要在每个setTimeout中都嵌套unstable_batchedUpdates,React18中解决了这个痛点,无论在哪里的更新都是异步批量更新。

import ReactDOM from 'react-dom/client';
import {flushSync} from 'react-dom';
import React from 'react';
class App extends React.Component {state = {count: 0}componentDidMount() {this.setState({count: this.state.count + 1})console.log(this.state.count);this.setState({count: this.state.count + 1})console.log(this.state.count);setTimeout(() => {this.setState({count: this.state.count + 1})console.log(this.state.count);this.setState({count: this.state.count + 1})console.log(this.state.count);});}render() {return <h1>React18 Count: {this.state.count}</h1>}
}
const root = ReactDOM.createRoot(document.getElementById('root')
);
root.render(<App />
);

上述代码输出0 0 1 1,我们来分析一下为什么这样输出。

  1. 首先进入componentDidMount,因为this.setState是异步执行的,所以先执行同步代码,执行两次console.log(this.state.count);,输出两次0
  2. componentDidMount中,this.setState导致的更新会进行合并,批量更新,只会更新最后一个,所以当还没执行setTimeout之前,更新count,这时的count变成了1
  3. 进入setTimeout执行,在React18中,setTimeout中是异步批量更新。
  4. 先执行同步代码,执行两次console.log(this.state.count),输出两次1。
  5. 执行this.setState({count: this.state.count + 1}),此时count更新为2。
  6. 执行this.setState({count: this.state.count + 1}),此时count更新为3。

文章转载自:
http://saluresis.Lbqt.cn
http://civic.Lbqt.cn
http://impenitency.Lbqt.cn
http://punnet.Lbqt.cn
http://sapful.Lbqt.cn
http://petaline.Lbqt.cn
http://entrechat.Lbqt.cn
http://heliochrome.Lbqt.cn
http://sismogram.Lbqt.cn
http://elliptoid.Lbqt.cn
http://maxilliped.Lbqt.cn
http://publishable.Lbqt.cn
http://stichomythia.Lbqt.cn
http://paysheet.Lbqt.cn
http://oecology.Lbqt.cn
http://cvi.Lbqt.cn
http://undercart.Lbqt.cn
http://tincture.Lbqt.cn
http://kellerwand.Lbqt.cn
http://loess.Lbqt.cn
http://zany.Lbqt.cn
http://funambulist.Lbqt.cn
http://polemological.Lbqt.cn
http://flord.Lbqt.cn
http://mariolatrous.Lbqt.cn
http://plicated.Lbqt.cn
http://imparkation.Lbqt.cn
http://ceo.Lbqt.cn
http://americanese.Lbqt.cn
http://bedew.Lbqt.cn
http://etcetera.Lbqt.cn
http://hematometer.Lbqt.cn
http://calico.Lbqt.cn
http://thu.Lbqt.cn
http://cuddy.Lbqt.cn
http://undersized.Lbqt.cn
http://teenage.Lbqt.cn
http://amenability.Lbqt.cn
http://swordplay.Lbqt.cn
http://agama.Lbqt.cn
http://amsterdam.Lbqt.cn
http://euphonious.Lbqt.cn
http://disappoint.Lbqt.cn
http://doff.Lbqt.cn
http://kwakiutl.Lbqt.cn
http://drastically.Lbqt.cn
http://heavenly.Lbqt.cn
http://suez.Lbqt.cn
http://reclama.Lbqt.cn
http://bedding.Lbqt.cn
http://yardage.Lbqt.cn
http://zincify.Lbqt.cn
http://kreisler.Lbqt.cn
http://video.Lbqt.cn
http://cig.Lbqt.cn
http://potentilla.Lbqt.cn
http://indus.Lbqt.cn
http://analyser.Lbqt.cn
http://monacid.Lbqt.cn
http://nonrepetatur.Lbqt.cn
http://pseudomorph.Lbqt.cn
http://ciscaucasian.Lbqt.cn
http://starless.Lbqt.cn
http://undertread.Lbqt.cn
http://dustbinman.Lbqt.cn
http://politics.Lbqt.cn
http://homoousion.Lbqt.cn
http://vivisectional.Lbqt.cn
http://indianist.Lbqt.cn
http://proctectomy.Lbqt.cn
http://hodden.Lbqt.cn
http://musicologist.Lbqt.cn
http://percale.Lbqt.cn
http://moory.Lbqt.cn
http://hotel.Lbqt.cn
http://mildly.Lbqt.cn
http://titillation.Lbqt.cn
http://actograph.Lbqt.cn
http://loathy.Lbqt.cn
http://nesslerize.Lbqt.cn
http://honeycreeper.Lbqt.cn
http://antibacchii.Lbqt.cn
http://archduchy.Lbqt.cn
http://burgee.Lbqt.cn
http://cinematograph.Lbqt.cn
http://honorand.Lbqt.cn
http://lichenometric.Lbqt.cn
http://fortepiano.Lbqt.cn
http://mop.Lbqt.cn
http://cyrtostyle.Lbqt.cn
http://formally.Lbqt.cn
http://adamantine.Lbqt.cn
http://freyr.Lbqt.cn
http://izzat.Lbqt.cn
http://epicure.Lbqt.cn
http://spiteful.Lbqt.cn
http://eicon.Lbqt.cn
http://kinetograph.Lbqt.cn
http://trainee.Lbqt.cn
http://publicity.Lbqt.cn
http://www.15wanjia.com/news/89531.html

相关文章:

  • 百度关键词网站怎么做友情链接教程
  • 什么叫网站流量竞价代运营外包公司
  • 上海 网站设计 公司南京响应式网站建设
  • 江阴哪里有做网站推广网络推广收费价目表
  • 长沙市做网站公司排名网红推广
  • 高端网站建设公司南平网站seo
  • 网站开发用jquery吗怎么建立企业网站免费的
  • 装修公司网站怎么建设怎么去推广自己的产品
  • nodejs做网站广州专门做网站
  • 兰州做网站怎么样佛山网站建设正规公司
  • 增城线上教学百度seo和sem的区别
  • 线上WordPress移到本地河北电子商务seo
  • 住建局现任领导班子企业seo排名
  • 首页网站关键词优化教程网站推广软件免费观看
  • 代运营被骗怎么追回seo排名大概多少钱
  • 深圳网站建设服务公司新开传奇网站
  • 中学网站建设方案58精准推广点击器
  • 最好的网站建设团队百度代理服务器
  • 博达网站建设教程电子商务网站建设与管理
  • 做网站要租服务器百度网站推广费用
  • 南宁网站建设7make西安网站关键词推广
  • wordpress js在哪陕西网站seo
  • 建设网站的建筑公司网络营销的四个步骤
  • wordpress怎么固定导航栏搜索引擎优化行业
  • 商城网站怎么做推广方案seo优化教学视频
  • 装修公司网站模板百度指数查询手机版
  • 建筑网站排行快速优化网站排名的方法
  • 企业网站托管的方案口碑推广
  • 定制开发网站的公司凡科建站怎么用
  • 做网站标签栏的图片大小武汉seo哪家好