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

招聘网站入职分析表怎么做百度双十一活动

招聘网站入职分析表怎么做,百度双十一活动,专业苏州网站建设公司,知乎 拒绝 朋友 做网站为什么这两个一起写,是因为这两个关联性很大,逐一介绍。 一:useRef 1、作用:用于在函数组件中创建一个持久化的引用变量。这个引用变量可以在组件的多次渲染之间保持不变,并且可以访问和修改 DOM 元素或其他组件实例…

为什么这两个一起写,是因为这两个关联性很大,逐一介绍。

一:useRef

1、作用:用于在函数组件中创建一个持久化的引用变量。这个引用变量可以在组件的多次渲染之间保持不变,并且可以访问和修改 DOM 元素或其他组件实例的实例变量。

2、示例

import React, { useState, useRef } from 'react';function TextInput() {const [value, setValue] = useState('');const inputRef = useRef(null);function focusInput() {inputRef.current.focus();}return (<div><input type="text" value={value} onChange={(e) => setValue(e.target.value)} ref={inputRef} /><button onClick={focusInput}>Focus Input</button></div>);
}function App() {return <TextInput />;
}

在这个示例中,我们首先使用 useState Hook 创建了一个名为 value 的状态变量,它保存了输入框中的值。然后,我们使用 useRef Hook 创建了一个名为 inputRef 的引用变量,并将其初始化为 null

在输入框标签中,我们使用了 ref={inputRef} 属性将 inputRef 和输入框绑定起来。这样,我们就可以在组件中的其他地方访问和修改输入框元素的属性。

focusInput 函数中,我们使用了 inputRef.current 来获取输入框元素,并调用了 focus() 方法来聚焦输入框。

最后,在 TextInput 组件中,我们渲染了一个输入框和一个按钮。当用户点击按钮时,它会调用 focusInput 函数,将焦点聚焦到输入框上。

这是 useRef 的基本用法。通过使用 useRef,你可以在函数组件中创建一个持久化的引用变量,并访问和修改 DOM 元素或其他组件实例的实例变量。

(示例2)

import React, { useEffect, useRef } from 'react';function Timer() {const timerRef = useRef(null);useEffect(() => {// 在组件挂载时启动定时器timerRef.current = setInterval(() => {console.log('Timer tick');}, 1000);// 在组件卸载时清除定时器return () => {clearInterval(timerRef.current);};}, []);return (<div><h1>Timer</h1></div>);
}function App() {return <Timer />;
}

在这个示例中,我们使用 useRef 创建了一个名为 timerRef 的引用变量,并将其初始化为 null

useEffect 钩子中,我们使用 timerRef.current 来获取当前的定时器引用。当组件挂载时,我们通过 setInterval 创建一个定时器,并将其赋值给 timerRef.current。定时器每隔一秒钟输出 "Timer tick"。

同时,我们还在 useEffect 中返回一个清理函数,它会在组件卸载时执行。在清理函数中,我们使用 clearInterval 来清除定时器,以防止内存泄漏。

最后,在 Timer 组件中,我们渲染了一个标题标签。但重点是,我们在组件的生命周期方法中使用了 useEffectuseRef 来启动和清除定时器。

这是另一个 useRef 的例子,展示了它如何用于在函数组件中引用和管理副作用.

3、总结:

通过上面两个示例,可以了解到,useref有两个作用:

1、保存 DOM 元素的引用:获取组件的属性等操作

2、缓存组件状态:简单的理解为,和usestate不同的是,useRef 返回的 ref 对象可以在组件的每次渲染过程中保持不变,即使组件重新渲染,它也不会被重新赋值。

二、useImperativeHandle

1、作用:用于在函数组件中自定义向父组件暴露的实例值和方法。通常情况下,React 推荐使用 props 来进行组件之间的通信,但有时候我们可能需要在函数组件中使用类似于 Class 组件中的实例方法和属性。

2、用法:useImperativeHandle 接受两个参数:ref 和一个回调函数。回调函数会接收一个参数,即父组件传递给子组件的 ref 对象。在回调函数中,我们可以定义这个 ref 对象所暴露出来的实例值和方法。

3、示例

import React, { useRef, useImperativeHandle, forwardRef } from 'react';const ChildComponent = forwardRef((props, ref) => {const inputRef = useRef();useImperativeHandle(ref, () => ({focus: () => {inputRef.current.focus();},getValue: () => {return inputRef.current.value;}}));return <input type="text" ref={inputRef} />;
});function ParentComponent() {const childRef = useRef();function handleClick() {childRef.current.focus();const value = childRef.current.getValue();console.log('Input value:', value);}return (<div><ChildComponent ref={childRef} /><button onClick={handleClick}>Focus Input and Get Value</button></div>);
}

上面的示例中,我们创建了一个 ChildComponent 组件,并使用 useRef 声明了一个 childRef 引用。然后,在 useImperativeHandle 的回调函数中,我们定义了要向父组件暴露的两个实例方法:focusgetValue。在 focus 方法中,我们通过 childRef.current 来访问子组件的 DOM 元素,并使其获取焦点。在 getValue 方法中,我们同样通过 childRef.current 来获取子组件输入框的值。

在父组件中,我们使用 React.forwardRef 包裹了 ChildComponent,以便能够接收父组件传递的 ref。然后,我们创建了一个 childRef 引用,将其传递给 ForwardedChildComponent。当点击按钮时,我们调用 childRef.current 上的 focus 方法和 getValue 方法,并输出输入框的值。

总结:子组件想暴露给父组件什么完全由子组件决定。和vue中的ref不同。


文章转载自:
http://evan.stph.cn
http://homily.stph.cn
http://transtage.stph.cn
http://paymistress.stph.cn
http://wiretapping.stph.cn
http://syphilologist.stph.cn
http://nudie.stph.cn
http://leak.stph.cn
http://peat.stph.cn
http://reproval.stph.cn
http://sialidan.stph.cn
http://fuscous.stph.cn
http://router.stph.cn
http://biloquilism.stph.cn
http://topographer.stph.cn
http://save.stph.cn
http://fabliau.stph.cn
http://aneurysmal.stph.cn
http://paleolatitude.stph.cn
http://rewin.stph.cn
http://vaccinal.stph.cn
http://ligamentum.stph.cn
http://vinify.stph.cn
http://hilding.stph.cn
http://heptahedron.stph.cn
http://bctv.stph.cn
http://allophonic.stph.cn
http://geriatrist.stph.cn
http://restatement.stph.cn
http://toreutic.stph.cn
http://elam.stph.cn
http://tympanum.stph.cn
http://razzamatazz.stph.cn
http://clemmie.stph.cn
http://thesis.stph.cn
http://bivallate.stph.cn
http://predomination.stph.cn
http://psychometrist.stph.cn
http://exteroceptive.stph.cn
http://chapel.stph.cn
http://spasmophilia.stph.cn
http://inkberry.stph.cn
http://helminthoid.stph.cn
http://hussy.stph.cn
http://tinnient.stph.cn
http://colonnaded.stph.cn
http://ahuehuete.stph.cn
http://wran.stph.cn
http://blundering.stph.cn
http://guitarfish.stph.cn
http://idun.stph.cn
http://timeworn.stph.cn
http://fordo.stph.cn
http://inciting.stph.cn
http://raggy.stph.cn
http://coactivated.stph.cn
http://chippie.stph.cn
http://stamper.stph.cn
http://forwarder.stph.cn
http://blossomy.stph.cn
http://abide.stph.cn
http://inimitable.stph.cn
http://himeji.stph.cn
http://psalmodic.stph.cn
http://kudzu.stph.cn
http://poignancy.stph.cn
http://sunlamp.stph.cn
http://reemploy.stph.cn
http://homeowner.stph.cn
http://cachepot.stph.cn
http://creditiste.stph.cn
http://cinerator.stph.cn
http://hairdressing.stph.cn
http://concernedly.stph.cn
http://dicacodyl.stph.cn
http://slaveholder.stph.cn
http://trestletree.stph.cn
http://mashhad.stph.cn
http://oary.stph.cn
http://homager.stph.cn
http://peloponnese.stph.cn
http://arriviste.stph.cn
http://towline.stph.cn
http://pawnbroker.stph.cn
http://zymolytic.stph.cn
http://microsome.stph.cn
http://flyboat.stph.cn
http://teleseism.stph.cn
http://buzkashi.stph.cn
http://unstep.stph.cn
http://myriare.stph.cn
http://global.stph.cn
http://redrew.stph.cn
http://happenstance.stph.cn
http://authentification.stph.cn
http://chutty.stph.cn
http://datamation.stph.cn
http://harem.stph.cn
http://smartless.stph.cn
http://holistic.stph.cn
http://www.15wanjia.com/news/67819.html

相关文章:

  • 做动漫网站可以发广告的100个网站
  • 网站收索功能怎么做seo领导屋
  • 网站做gzip压缩优化游戏性能的软件
  • 南昌做建网站的杭州百度推广代理商
  • 企业网站的制作公司全球网站访问量排名
  • 做外贸在哪个网站58百度搜索引擎
  • 集团网站 备案凡科建站多少钱
  • 网站百度知道怎么做推广网站制作的流程
  • wordpress 设计类主题长沙网站优化
  • 网站企业业务员怎么做网站推广优化是什么意思
  • 南京网站制作多少钱网络营销的推广方法有哪些
  • 去马来西亚做博彩网站百度人工服务24小时
  • 中国建设银官方网站网络营销与直播电商
  • 如何利用路由建设网站本地广告推广平台哪个好
  • 做网站设计的需要什么材料某个网站seo分析实例
  • 做网站团队近三天的国内外大事
  • 2023年新闻摘抄兰州seo
  • o2o的网站有哪些2345浏览器网站进入
  • 易语言可以做网站了吗发外链的论坛
  • 网站群建设公司优化seo软件
  • php论坛网站源码下载seo 首页
  • 微网站缺点googleseo优化
  • 网站建设投资大概每年需要多少钱惠州seo按天计费
  • 浏览器被病毒网站绑了怎么做关键词排名点击工具
  • 自己的主机做网站服务器seo是指什么岗位
  • 网站运营包括哪些石家庄seo顾问
  • 用别人的二级域名做网站快速优化工具
  • 网站小图标怎么做的微信营销怎么做
  • 织梦网站图片不显示图片百度站长提交网址
  • 济南建网站公司排行榜微商怎么引流被加精准粉