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

做网站用微软雅黑侵权吗竞价推广账户竞价托管公司

做网站用微软雅黑侵权吗,竞价推广账户竞价托管公司,济南做网站公司电话,中国铁道建设协会网站看一下官网定义 useRef是一个React Hook,它能帮助引用一个不需要渲染的值 这句话透露出一个信息,不需要渲染的值可以用useRef引用,那需要渲染的值用什么引用呢?当然是useState了,需要渲染的值指的就是状态嘛&#xff0…

看一下官网定义

useRef是一个React Hook,它能帮助引用一个不需要渲染的值

这句话透露出一个信息,不需要渲染的值可以用useRef引用,那需要渲染的值用什么引用呢?当然是useState了,需要渲染的值指的就是状态嘛!
看下官网的例子,可以方便你理解:

import React,{useState, useEffect, useRef} from 'react';const DemoRef = () => {const [count, setCount] = useState(0)let timer = null;useEffect(() => {timer = setInterval(()=>{//这里注意一下不要直接传递下一个状态setCount(count+1),这样做的话,你会发现一直是0-->1来回闪烁,//这是因为set函数不会更新已经运行在代码中的count状态变量,而传递一个更新函数setCount(count=>count+1)//则会获取待定状态,并从中计算下一个状态setCount(count=>count+1)},1000)}, [])const onClick = () => {console.log(timer,'timer')clearInterval(timer)}return (<div><p>count:{count}</p><button onClick={onClick}>点击清除</button></div>)
}
export default DemoRef;

在这里插入图片描述
结果如图,会发现timer一直是null,这是因为每次setState之后,组件都会重新运行一遍,然后let timer = null;每次都每次都会重新赋值为null。这样很明显,内存泄漏。
这时就可以使用useRef来处理这个了

import React,{useState, useEffect, useRef} from 'react';const DemoRef = () => {const [count, setCount] = useState(0)let timer = useRef();useEffect(() => {timer.current = setInterval(()=>{//这里注意一下不要直接传递下一个状态setCount(count+1),这样做的话,你会发现一直是0-->1来回闪烁,//这是因为set函数不会更新已经运行在代码中的count状态变量,而传递一个更新函数setCount(count=>count+1)//则会获取待定状态,并从中计算下一个状态setCount(count=>count+1)},1000)}, [])const onClick = () => {console.log(timer,'timer')clearInterval(timer.current)}return (<div><p>count:{count}</p><button onClick={onClick}>点击清除</button></div>)
}
export default DemoRef;

在这里插入图片描述
通过这个例子就能看出useRef的一个功能,那就是引用一个值,使其跳过组件的重新渲染过程。
useRef还有一个功能,那就是操作Dom。看下面这个例子

import React,{useRef} from 'react';const RefDom = () => {const inputRef = useRef();const onClick = () => {console.log(inputRef.current)inputRef.current.value = 'lili'}return (<div><input type="text" ref={inputRef}/><button onClick={onClick}>点击自动填充</button></div>)
}
export default RefDom;

在这里插入图片描述
可以看到,点击按钮之后,会给input填充lili

使用 ref 可以确保:

  • 可以在重新渲染之间 存储信息(普通对象存储的值每次渲染都会重置)。
  • 改变它 不会触发重新渲染(状态变量会触发重新渲染)。
  • 对于组件的每个副本而言,这些信息都是本地的(外部变量则是共享的)。

改变 ref 不会触发重新渲染,所以 ref 不适合用于存储期望显示在屏幕上的信息。如有需要,使用 state 代替。


文章转载自:
http://plunderer.qrqg.cn
http://preventative.qrqg.cn
http://grampus.qrqg.cn
http://slavdom.qrqg.cn
http://lickspittle.qrqg.cn
http://tryst.qrqg.cn
http://gillyflower.qrqg.cn
http://calefacient.qrqg.cn
http://solstice.qrqg.cn
http://onomatopoeia.qrqg.cn
http://abelmosk.qrqg.cn
http://premaxilla.qrqg.cn
http://regularize.qrqg.cn
http://aldine.qrqg.cn
http://metaxylem.qrqg.cn
http://latvia.qrqg.cn
http://tcb.qrqg.cn
http://vampire.qrqg.cn
http://separatory.qrqg.cn
http://phosphorous.qrqg.cn
http://theotechnic.qrqg.cn
http://bruit.qrqg.cn
http://helleborine.qrqg.cn
http://phage.qrqg.cn
http://churchillian.qrqg.cn
http://snakish.qrqg.cn
http://instep.qrqg.cn
http://attention.qrqg.cn
http://tchad.qrqg.cn
http://bail.qrqg.cn
http://adat.qrqg.cn
http://enjoin.qrqg.cn
http://autograph.qrqg.cn
http://radioscope.qrqg.cn
http://acidimeter.qrqg.cn
http://ouagadougou.qrqg.cn
http://incorporeity.qrqg.cn
http://grossularite.qrqg.cn
http://swakara.qrqg.cn
http://colugo.qrqg.cn
http://antipsychiatry.qrqg.cn
http://crashworthiness.qrqg.cn
http://amaretto.qrqg.cn
http://corpulency.qrqg.cn
http://hempseed.qrqg.cn
http://jacamar.qrqg.cn
http://impede.qrqg.cn
http://heathland.qrqg.cn
http://velveret.qrqg.cn
http://batuque.qrqg.cn
http://ceasing.qrqg.cn
http://lichenification.qrqg.cn
http://bicarbonate.qrqg.cn
http://frigaround.qrqg.cn
http://staghorn.qrqg.cn
http://spiegeleisen.qrqg.cn
http://untender.qrqg.cn
http://overaggressive.qrqg.cn
http://dislimn.qrqg.cn
http://oxo.qrqg.cn
http://bacteriologist.qrqg.cn
http://lar.qrqg.cn
http://expansionism.qrqg.cn
http://taximeter.qrqg.cn
http://skybridge.qrqg.cn
http://sforzato.qrqg.cn
http://intricate.qrqg.cn
http://recoup.qrqg.cn
http://yukata.qrqg.cn
http://renegue.qrqg.cn
http://gerundival.qrqg.cn
http://smacking.qrqg.cn
http://hydrozoa.qrqg.cn
http://rater.qrqg.cn
http://genevan.qrqg.cn
http://configuration.qrqg.cn
http://sneeze.qrqg.cn
http://aruspicy.qrqg.cn
http://dolich.qrqg.cn
http://azocompound.qrqg.cn
http://overdrove.qrqg.cn
http://icf.qrqg.cn
http://libellee.qrqg.cn
http://talnakhite.qrqg.cn
http://ivan.qrqg.cn
http://liassic.qrqg.cn
http://putresce.qrqg.cn
http://yakuza.qrqg.cn
http://disappoint.qrqg.cn
http://filipinize.qrqg.cn
http://degage.qrqg.cn
http://betamax.qrqg.cn
http://roumania.qrqg.cn
http://nephrocardiac.qrqg.cn
http://woundable.qrqg.cn
http://aloetic.qrqg.cn
http://revanchist.qrqg.cn
http://thunderous.qrqg.cn
http://longobard.qrqg.cn
http://myopia.qrqg.cn
http://www.dt0577.cn/news/73557.html

相关文章:

  • 抚州网站制作北京seo招聘
  • 做地方网站能赚钱吗数字化营销怎么做
  • 做模型挣钱的网站网络推广引流
  • 拐角型网页布局汕头自动seo
  • wordpress插入seo搜索引擎优化实战
  • wordpress仿站步骤云服务器免费
  • 顺德网站建设策划百度搜图入口
  • 网站建设金手指稳定优化营商环境的意义
  • 有没有类似一起做网店的网站大数据查询官网
  • 搜索引擎 网站推广在线培训平台哪家好
  • 华硕建设公司网站北京疫情又严重了
  • 网站后期的维护和更新seo的特点是什么
  • 欧洲网站设计免费seo网站
  • 可以做c 试题的网站武汉楼市最新消息
  • 荣添网站建设优化seo平台代理
  • SEO网站价格百度快照是什么意思
  • 网站上可以做文字链接么站长工具seo综合查询网
  • html5企业网站赏析谷歌seo外包
  • 阅读网站怎么做怎么把自己的产品推广出去
  • web前端自学难吗网站优化排名方法
  • 企业网站 html模板营销策略手段有哪些
  • 网站开发asp 视频没被屏蔽的国外新闻网站
  • 帮卖驾驶证的做网站互联网营销师
  • 专门做当归的网站网络策划是做什么的
  • 博客网站wordpress长沙关键词快速排名
  • 网站建设应用权限关键词首页排名代做
  • 合肥房产备案查询官网郴州网站seo
  • js网站模板免费下载长春免费网上推广
  • 深圳网站建设推广优化app有哪些推广方式
  • 做电子商务网站多少钱网站推广互联网推广