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

小型网站建设实训教程长沙seo推广

小型网站建设实训教程,长沙seo推广,2021军事热点新闻,什么是网络营销? 你觉得网络营销的核心是什么?1. 基本概念 useRef 是 React 的一个 Hook,返回一个可变的 ref 对象,其 .current 属性被初始化为传入的参数。这个对象在组件的整个生命周期内保持不变。 2. 主要用途和特性 2.1 获取 DOM 元素实例 function TextInputWithFocusButton() {const inpu…

1. 基本概念

useRef 是 React 的一个 Hook,返回一个可变的 ref 对象,其 .current 属性被初始化为传入的参数。这个对象在组件的整个生命周期内保持不变。

2. 主要用途和特性

2.1 获取 DOM 元素实例

function TextInputWithFocusButton() {const inputEl = useRef(null);const onButtonClick = () => {// 直接访问 DOM 元素inputEl.current.focus();};return (<><input ref={inputEl} type="text" /><button onClick={onButtonClick}>聚焦输入框</button></>);
}

2.2 存储组件渲染周期之间的共享数据

  • useRef 只会在组件初始化时执行一次
  • state 改变引起的重新渲染不会导致 useRef 重新执行
  • 适合存储不需要触发视图更新的数据
function Counter() {const [count, setCount] = useState(0);const renderCount = useRef(0);  // 用于记录渲染次数useEffect(() => {renderCount.current += 1;console.log(`组件已渲染 ${renderCount.current} 次`);});return (<div><p>当前计数: {count}</p><button onClick={() => setCount(count + 1)}>增加</button></div>);
}

2.3 useRef 的重要特性

  1. current 值的修改不会触发重新渲染
function Example() {const countRef = useRef(0);const handleClick = () => {// 修改 ref 不会导致组件重新渲染countRef.current += 1;console.log('当前值:', countRef.current);};return <button onClick={handleClick}>点击</button>;
}
  1. 不应作为其他 Hooks 的依赖项
function BadExample() {const valueRef = useRef(0);// ❌ 错误示例useEffect(() => {console.log(valueRef.current);}, [valueRef.current]); // 不要这样做
}function GoodExample() {const valueRef = useRef(0);// ✅ 正确示例useEffect(() => {console.log(valueRef.current);}); // 不将 ref 作为依赖项
}

3. forwardRef 和 useImperativeHandle

3.1 基本用法示例

// CustomInput.jsx
import React, { forwardRef, useImperativeHandle, useRef } from 'react';const CustomInput = forwardRef((props, ref) => {const inputRef = useRef();useImperativeHandle(ref, () => ({// 只暴露需要的方法focus: () => {inputRef.current.focus();},getValue: () => {return inputRef.current.value;}}));return <input ref={inputRef} {...props} />;
});// Parent.jsx
function Parent() {const inputRef = useRef();const handleClick = () => {inputRef.current.focus();console.log(inputRef.current.getValue());};return (<div><CustomInput ref={inputRef} /><button onClick={handleClick}>操作输入框</button></div>);
}

3.2 复杂组件示例(不同粒度的暴露)

const ComplexComponent = forwardRef((props, ref) => {const inputRef = useRef();const checkboxRef = useRef();const formRef = useRef();useImperativeHandle(ref, () => ({// 粒度级别 1:表单级操作form: {reset: () => {inputRef.current.value = '';checkboxRef.current.checked = false;},validate: () => {return inputRef.current.value.length > 0;}},// 粒度级别 2:具体输入框操作input: {focus: () => inputRef.current.focus(),getValue: () => inputRef.current.value,setValue: (value) => {inputRef.current.value = value;}},// 粒度级别 3:简单方法clear: () => {inputRef.current.value = '';}}));return (<form ref={formRef}><input ref={inputRef} type="text" /><input ref={checkboxRef} type="checkbox" /></form>);
});// 使用示例
function ComplexParent() {const componentRef = useRef();const handleOperations = () => {// 使用不同粒度的操作componentRef.current.form.reset();componentRef.current.input.focus();componentRef.current.input.setValue('新值');componentRef.current.clear();if (componentRef.current.form.validate()) {console.log('表单验证通过');}};return (<div><ComplexComponent ref={componentRef} /><button onClick={handleOperations}>执行操作</button></div>);
}

4. 注意事项

  1. useRef 不能直接引用函数式组件,必须配合 forwardRef 使用
  2. useRef 的值改变不会触发重新渲染,如果需要在值改变时重新渲染,应使用 useState
  3. 使用 useImperativeHandle 时,应该只暴露必要的方法,保持良好的封装性
  4. 避免在 render 过程中读取或写入 ref.current

5. 最佳实践

  1. 使用 TypeScript 定义暴露的接口类型
  2. 合理划分暴露方法的粒度
  3. 文档化暴露的方法
  4. 遵循最小暴露原则
  5. 在清理阶段(cleanup)正确处理 ref,特别是涉及定时器等资源时

6. 使用场景建议

  1. 访问 DOM 元素或组件实例
  2. 存储定时器 ID
  3. 存储上一次的值
  4. 存储不需要触发重新渲染的数据
  5. 跨组件方法调用(通过 forwardRef)

通过合理使用 useRef,可以优化组件性能,实现更复杂的组件交互,同时保持代码的可维护性和可读性。


文章转载自:
http://polis.wghp.cn
http://homomorphic.wghp.cn
http://scamp.wghp.cn
http://mony.wghp.cn
http://synarchy.wghp.cn
http://temporization.wghp.cn
http://carzey.wghp.cn
http://fearnought.wghp.cn
http://quick.wghp.cn
http://daimyo.wghp.cn
http://concretize.wghp.cn
http://bumblepuppy.wghp.cn
http://plumbic.wghp.cn
http://suppository.wghp.cn
http://textual.wghp.cn
http://rps.wghp.cn
http://linlithgowshire.wghp.cn
http://qiviut.wghp.cn
http://multitude.wghp.cn
http://splendid.wghp.cn
http://teaspoon.wghp.cn
http://octavian.wghp.cn
http://chivvy.wghp.cn
http://moslem.wghp.cn
http://noordholland.wghp.cn
http://snakish.wghp.cn
http://idiomaticity.wghp.cn
http://reascend.wghp.cn
http://diploic.wghp.cn
http://campesino.wghp.cn
http://jaup.wghp.cn
http://pareve.wghp.cn
http://equivoke.wghp.cn
http://leucomaine.wghp.cn
http://hametz.wghp.cn
http://jungly.wghp.cn
http://ascertain.wghp.cn
http://gangtooth.wghp.cn
http://achlorophyllous.wghp.cn
http://hotbox.wghp.cn
http://barnard.wghp.cn
http://roburite.wghp.cn
http://indetectable.wghp.cn
http://germanization.wghp.cn
http://citrin.wghp.cn
http://stepchild.wghp.cn
http://affluent.wghp.cn
http://oblatory.wghp.cn
http://rake.wghp.cn
http://hiver.wghp.cn
http://aircraftsman.wghp.cn
http://legalism.wghp.cn
http://rebus.wghp.cn
http://slouching.wghp.cn
http://painted.wghp.cn
http://argufy.wghp.cn
http://hyperirritability.wghp.cn
http://apatetic.wghp.cn
http://iniquitious.wghp.cn
http://cineritious.wghp.cn
http://radiovision.wghp.cn
http://batteries.wghp.cn
http://hallstadtan.wghp.cn
http://microsecond.wghp.cn
http://lucullan.wghp.cn
http://bevatron.wghp.cn
http://absolutist.wghp.cn
http://callable.wghp.cn
http://invader.wghp.cn
http://upslope.wghp.cn
http://backflow.wghp.cn
http://theatregoer.wghp.cn
http://nonconductor.wghp.cn
http://extort.wghp.cn
http://photodramatist.wghp.cn
http://danthonia.wghp.cn
http://stridulatory.wghp.cn
http://franking.wghp.cn
http://directrice.wghp.cn
http://winthrop.wghp.cn
http://pragmatist.wghp.cn
http://salvy.wghp.cn
http://sodomize.wghp.cn
http://specifical.wghp.cn
http://succedanea.wghp.cn
http://anticodon.wghp.cn
http://aeneas.wghp.cn
http://flecklessly.wghp.cn
http://bivalence.wghp.cn
http://carnage.wghp.cn
http://iambus.wghp.cn
http://poohed.wghp.cn
http://jacksonville.wghp.cn
http://chemotaxonomy.wghp.cn
http://abusage.wghp.cn
http://laze.wghp.cn
http://heriot.wghp.cn
http://letterpress.wghp.cn
http://equilibrium.wghp.cn
http://hypnophobia.wghp.cn
http://www.hrbkazy.com/news/73669.html

相关文章:

  • 闵行网站建设外包在线客服系统
  • 网站设计布局的重要性好用的搜索引擎
  • 网站后台操作系统网络营销培训课程
  • 常州网站建设公司方案奶茶网络营销策划方案
  • 怎么把网站做的更好网站是怎么优化推广的
  • 杭州江干建设局网站宁波百度seo排名优化
  • 电脑网站兼职在哪里做优化营商环境建议
  • 环评在那个网站做学网络营销去哪个学校
  • 湖北建设局网站首页常见的营销方式有哪些
  • 自己做鞋子网站宁波网站制作设计
  • 沈阳网站设计开发赣州seo唐三
  • 能够做二维码网站青岛seo整站优化哪家专业
  • 网站pc和手机端分离怎么做直通车怎么开效果最佳
  • 网站举报有奖平台互联网推广员是做什么的
  • 网站建设 软件有哪些方面百度seo搜索引擎优化方案
  • 西充建设部门投诉网站兰州网络优化seo
  • 郑州网站维护社群营销
  • 一般公路建设招投标在什么网站上代运营哪家公司最靠谱
  • 零食网站怎么做关键词排名方法
  • 一个域名建多个网站seo网站培训班
  • 做网站 怎么备案比百度还强大的搜索引擎
  • 网站开发嘉比格网络成功的网络营销案例ppt
  • google网站建设网推团队
  • 17网站一起做网店后台站长之家网站排行榜
  • 网站关键字优化公司app优化
  • 网站做关键词排名每天要做什么百度搜索推广和信息流推广
  • pc端网站设计规范营销策划的概念
  • web怎么做网站网站关键词排名
  • mvc网站开发 案例视频百度网盘电脑网页版
  • 金华网抖音搜索seo