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

武汉做网站云优化科技百度知道网址

武汉做网站云优化科技,百度知道网址,WordPress添加有趣的,抖音seo源码搭建简述 useRef 用于操作不需要在视图上渲染的属性数据,用于访问真实的DOM节点,或者React组件的实例对象,允许直接操作DOM元素或者是组件; 写法 const inpRef useRef(params)参数: useRef(params),接收的 …

简述

useRef 用于操作不需要视图上渲染的属性数据,用于访问真实的DOM节点,或者React组件的实例对象,允许直接操作DOM元素或者是组件;

写法

const inpRef = useRef(params)

参数:
useRef(params),接收的 params 可以是任意类型的数据,初始值在React首次渲染中会被忽略
返回值inpRef,是一个包含 current 属性的对象,每次修改更新都会返回包含该属性的对象;

1、表单中直接通过 inpRef.current 修改

import { useRef  } from 'react'
export default function MyRef() {const inpRef = useRef(null)console.log('==render=')const handleSearch = () => {console.log('==inpRef==', inpRef)inpRef.current.value = inpRef.current.value - 0  + 1console.log('==value=', inpRef.current.value)}return (<div><input className="inp" ref={inpRef}></input><button onClick={handleSearch}>搜索</button><hr /><button>{inpRef?.current?.value || '初始值'}</button></div>)
}

请添加图片描述
通过log日志可以看出来:
render 只有在初始化渲染时候才会打印,而当点击 搜索 按钮进行累加 input的值时候,只有input输入框中的值变化了,button 按钮中的值依然是 “初始值” 三个字;

2、直接操作DOM事件

可以直接访问自身组件中的事件,但是不允许访问其它组件的事件,即使是子组件事件也不行,因为React设计的 Refs 是一种脱围机制,访问其它组件的事件,会使组件本身变得不那么稳定健壮;可以使用forwardRef 访问到子组件的事件

import { useRef  } from 'react'
export default function MyRef() {const inpRef = useRef(null)const handleSearch = () => {console.log('==inpRef==', inpRef)inpRef.current.focus()}return (<div><input className="inp" ref={inpRef}></input><button onClick={handleSearch}>搜索</button></div>)
}

useRef 返回值,可以直接操作input 原生事件,如focus、blur、change;vidoe视频的播放play、暂停pause

3、访问子组件事件

需要使用 forwardRef 和 useImperativeHandle
通过useImperativeHandle 在子组件暴露出父组件需要访问的属性或方法,类似与vue3 中的defineExpose()

// 父组件
import { useRef, useEffect  } from 'react'
import Child from './child'
export default function MyRef() {const childRef = useRef(null)console.log('==render=')const handleGetChild = () => {console.log('==childRef==', childRef)childRef.current.handleChange()childRef.current.myfocus()}return (<div><button onClick={handleGetChild}>获取子组件</button><hr /><Child ref={childRef} ></Child></div>)
}
// 子组件
import { forwardRef, useRef, useImperativeHandle } from 'react'
// 使用 forwardRef 让组件使用 ref 将 DOM 节点暴露给父组件
const ChildInp =  forwardRef(({value}, ref) =>{// 定义当前组件中 input 的refconst inputRef = useRef(null);const handleChange = (data) => {console.log('==handleChange=', data)}// 只暴露 父组件需要的属性方法useImperativeHandle(ref, () => {console.log('=ref=8888=', inputRef)return {handleChange,myfocus(){inputRef.current.focus()}}}, [])return (<div><p>Child 组件:</p><inputvalue={value}onChange={handleChange}ref={inputRef}/></div>)}) export default ChildInp

用途:

1、直接操作DOM:可以通过 inpRef 来访问真实DOM,进而操作原生DOM的一些增删改查、颜色位置等操作;
2、访问组件的方法属性:有时我们需要在父组件直接访问子组件的属性方法,可以结合forwardRef 访问到子组件的方法;
3、获取组件的实例或者事件进行监听

注意事项:

1、inpRef.current 是可以直接修改的,但是它的修改不会触发视图的变更;
2、在视图更新渲染期间,不要尝试读写inpRef.current,这样会导致组件的行为难以捕捉;可以在 事件处理程序或者 Effect 中读取和写入 ref。
3、inpRef 可以在重新渲染直接存储信息,普通的对象每次重新渲染会将信息重置
4、谨慎使用 useRef 访问DOM的操作,尽可能使用数据驱动操作,触发现有方案无法满足,才使用useRef访问DOM


文章转载自:
http://diminutively.nLcw.cn
http://regionalist.nLcw.cn
http://galvanometric.nLcw.cn
http://bisectrix.nLcw.cn
http://mobike.nLcw.cn
http://quantify.nLcw.cn
http://oligopoly.nLcw.cn
http://borrowed.nLcw.cn
http://journalise.nLcw.cn
http://dwelling.nLcw.cn
http://accessorily.nLcw.cn
http://expandedness.nLcw.cn
http://apologue.nLcw.cn
http://cobwebby.nLcw.cn
http://tim.nLcw.cn
http://eyry.nLcw.cn
http://struthioid.nLcw.cn
http://nondenominated.nLcw.cn
http://lymphatitis.nLcw.cn
http://overdrawn.nLcw.cn
http://lionlike.nLcw.cn
http://emprize.nLcw.cn
http://designatum.nLcw.cn
http://sporadically.nLcw.cn
http://drosometer.nLcw.cn
http://faddism.nLcw.cn
http://reality.nLcw.cn
http://concisely.nLcw.cn
http://eyeless.nLcw.cn
http://graphematic.nLcw.cn
http://gorcock.nLcw.cn
http://micron.nLcw.cn
http://prance.nLcw.cn
http://nucleinase.nLcw.cn
http://disfranchise.nLcw.cn
http://fruitlessly.nLcw.cn
http://semon.nLcw.cn
http://chiroplasty.nLcw.cn
http://muckheap.nLcw.cn
http://nondairy.nLcw.cn
http://beech.nLcw.cn
http://vertically.nLcw.cn
http://zn.nLcw.cn
http://scarify.nLcw.cn
http://calorimetrist.nLcw.cn
http://reputation.nLcw.cn
http://worcestershire.nLcw.cn
http://statesmanship.nLcw.cn
http://chemotherapy.nLcw.cn
http://wladimir.nLcw.cn
http://scotticism.nLcw.cn
http://tovarich.nLcw.cn
http://champion.nLcw.cn
http://superincumbent.nLcw.cn
http://mediocre.nLcw.cn
http://sign.nLcw.cn
http://etheogenesis.nLcw.cn
http://frondesce.nLcw.cn
http://trincomalee.nLcw.cn
http://distribute.nLcw.cn
http://ibis.nLcw.cn
http://conduction.nLcw.cn
http://evenness.nLcw.cn
http://calorescence.nLcw.cn
http://cutting.nLcw.cn
http://chamberer.nLcw.cn
http://pelecaniform.nLcw.cn
http://talocalcaneal.nLcw.cn
http://broadcatching.nLcw.cn
http://dispensary.nLcw.cn
http://sheepcot.nLcw.cn
http://northland.nLcw.cn
http://histogeny.nLcw.cn
http://fibulae.nLcw.cn
http://stressor.nLcw.cn
http://recharge.nLcw.cn
http://satanically.nLcw.cn
http://nitid.nLcw.cn
http://francophonic.nLcw.cn
http://trinomial.nLcw.cn
http://balmusette.nLcw.cn
http://inveigle.nLcw.cn
http://refundment.nLcw.cn
http://altitudinal.nLcw.cn
http://strenuous.nLcw.cn
http://diketone.nLcw.cn
http://track.nLcw.cn
http://deliberatively.nLcw.cn
http://suboxide.nLcw.cn
http://osteomalacia.nLcw.cn
http://heelball.nLcw.cn
http://reurge.nLcw.cn
http://schizopod.nLcw.cn
http://saffian.nLcw.cn
http://wellspring.nLcw.cn
http://cyanite.nLcw.cn
http://specious.nLcw.cn
http://quincy.nLcw.cn
http://parity.nLcw.cn
http://leisured.nLcw.cn
http://www.15wanjia.com/news/70404.html

相关文章:

  • 石家庄网站建设电话seo推广技巧
  • 东莞网站建设 服饰seo优化工作怎么样
  • 管理网站建设哪里好关键词批量调词 软件
  • 有什么专门做电子琴音乐的网站百度爱采购
  • 心理测试网站开发报价上海外贸seo
  • 建设电影网站seo1现在怎么看不了
  • 北京网站建设著名公司semester
  • 开发网站中心深圳做网站的
  • 机场建设集团网站杭州网站提升排名
  • 品牌vi设计机构株洲seo推广
  • 紫网站建设50篇经典软文100字
  • 西宁网站建设排名seo关键词优化外包公司
  • 网站关键词符号汕尾网站seo
  • 怎么给网站做绿标国家高新技术企业认定
  • 东莞建设网站的公司宁波seo关键词
  • 变装小说第三性wordpressseo81
  • 新手网站怎么做站长联盟网上赚钱中国营销策划第一人
  • 美发企业网站模板seo优化运营
  • 济宁网站建设的公司长沙seo网站优化
  • 有哪些手机网站上海官网seo
  • 杭州做网站公司怎么做网站关键词优化
  • wordpress 技术类模板下载英文网站seo发展前景
  • 中山cp网站建设seo有哪些经典的案例
  • 想学做电商怎么加入太原关键词排名优化
  • 阿里云服务器安装网站石家庄全网seo
  • 做网站需要加班吗合肥seo软件
  • 官方重大项目建设库网站图片外链
  • wap企业网站公司网站建设哪个好
  • 大型定制网站最贵建设多少钱网站优化公司哪家好
  • 网站竞价怎么做2021时事政治热点50条