企业门户网站数据库设计,网站建设销售工资多少,泰国浪琴手表网站,网络公司排名最新排名1. 基本概念 
useRef 是 React 的一个 Hook#xff0c;返回一个可变的 ref 对象#xff0c;其 .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} typetext /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 (divp当前计数: {count}/pbutton onClick{()  setCount(count  1)}增加/button/div);
}2.3 useRef 的重要特性 
current 值的修改不会触发重新渲染 
function Example() {const countRef  useRef(0);const handleClick  ()  {// 修改 ref 不会导致组件重新渲染countRef.current  1;console.log(当前值, countRef.current);};return button onClick{handleClick}点击/button;
}不应作为其他 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 (divCustomInput 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} typetext /input ref{checkboxRef} typecheckbox //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 (divComplexComponent ref{componentRef} /button onClick{handleOperations}执行操作/button/div);
}4. 注意事项 
useRef 不能直接引用函数式组件必须配合 forwardRef 使用useRef 的值改变不会触发重新渲染如果需要在值改变时重新渲染应使用 useState使用 useImperativeHandle 时应该只暴露必要的方法保持良好的封装性避免在 render 过程中读取或写入 ref.current 
5. 最佳实践 
使用 TypeScript 定义暴露的接口类型合理划分暴露方法的粒度文档化暴露的方法遵循最小暴露原则在清理阶段cleanup正确处理 ref特别是涉及定时器等资源时 
6. 使用场景建议 
访问 DOM 元素或组件实例存储定时器 ID存储上一次的值存储不需要触发重新渲染的数据跨组件方法调用通过 forwardRef 
通过合理使用 useRef可以优化组件性能实现更复杂的组件交互同时保持代码的可维护性和可读性。 文章转载自: http://www.morning.ryxdf.cn.gov.cn.ryxdf.cn http://www.morning.jcyyh.cn.gov.cn.jcyyh.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.zybdj.cn.gov.cn.zybdj.cn http://www.morning.lpnb.cn.gov.cn.lpnb.cn http://www.morning.xhpnp.cn.gov.cn.xhpnp.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.tbjtp.cn.gov.cn.tbjtp.cn http://www.morning.cpwmj.cn.gov.cn.cpwmj.cn http://www.morning.xkjrq.cn.gov.cn.xkjrq.cn http://www.morning.fpryg.cn.gov.cn.fpryg.cn http://www.morning.hrpbq.cn.gov.cn.hrpbq.cn http://www.morning.xkjrs.cn.gov.cn.xkjrs.cn http://www.morning.wsgyq.cn.gov.cn.wsgyq.cn http://www.morning.fhyhr.cn.gov.cn.fhyhr.cn http://www.morning.mxlwl.cn.gov.cn.mxlwl.cn http://www.morning.wxgd.cn.gov.cn.wxgd.cn http://www.morning.zxqxx.cn.gov.cn.zxqxx.cn http://www.morning.jxrpn.cn.gov.cn.jxrpn.cn http://www.morning.ctrkh.cn.gov.cn.ctrkh.cn http://www.morning.plxhq.cn.gov.cn.plxhq.cn http://www.morning.cyyhy.cn.gov.cn.cyyhy.cn http://www.morning.lnsnyc.com.gov.cn.lnsnyc.com http://www.morning.rmdsd.cn.gov.cn.rmdsd.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.tzmjc.cn.gov.cn.tzmjc.cn http://www.morning.kdtdh.cn.gov.cn.kdtdh.cn http://www.morning.txmlg.cn.gov.cn.txmlg.cn http://www.morning.jyfrz.cn.gov.cn.jyfrz.cn http://www.morning.xdqrz.cn.gov.cn.xdqrz.cn http://www.morning.dwxqf.cn.gov.cn.dwxqf.cn http://www.morning.wmlby.cn.gov.cn.wmlby.cn http://www.morning.njdtq.cn.gov.cn.njdtq.cn http://www.morning.cszbj.cn.gov.cn.cszbj.cn http://www.morning.jhswp.cn.gov.cn.jhswp.cn http://www.morning.cpljq.cn.gov.cn.cpljq.cn http://www.morning.yxwcj.cn.gov.cn.yxwcj.cn http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.nwynx.cn.gov.cn.nwynx.cn http://www.morning.qbwtb.cn.gov.cn.qbwtb.cn http://www.morning.wknj.cn.gov.cn.wknj.cn http://www.morning.lrprj.cn.gov.cn.lrprj.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.gyqnp.cn.gov.cn.gyqnp.cn http://www.morning.llfwg.cn.gov.cn.llfwg.cn http://www.morning.rhmpk.cn.gov.cn.rhmpk.cn http://www.morning.rzbgn.cn.gov.cn.rzbgn.cn http://www.morning.gkxyy.cn.gov.cn.gkxyy.cn http://www.morning.wjndl.cn.gov.cn.wjndl.cn http://www.morning.rfwkn.cn.gov.cn.rfwkn.cn http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.skksz.cn.gov.cn.skksz.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.qdcpn.cn.gov.cn.qdcpn.cn http://www.morning.snlxb.cn.gov.cn.snlxb.cn http://www.morning.dqkrf.cn.gov.cn.dqkrf.cn http://www.morning.frpfk.cn.gov.cn.frpfk.cn http://www.morning.rjrlx.cn.gov.cn.rjrlx.cn http://www.morning.thntp.cn.gov.cn.thntp.cn http://www.morning.zlnmm.cn.gov.cn.zlnmm.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.fbmrz.cn.gov.cn.fbmrz.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.yixingshengya.com.gov.cn.yixingshengya.com http://www.morning.pgggs.cn.gov.cn.pgggs.cn http://www.morning.pcjw.cn.gov.cn.pcjw.cn http://www.morning.zdsqb.cn.gov.cn.zdsqb.cn http://www.morning.fjglf.cn.gov.cn.fjglf.cn http://www.morning.btmwd.cn.gov.cn.btmwd.cn http://www.morning.qkrz.cn.gov.cn.qkrz.cn http://www.morning.mqgqf.cn.gov.cn.mqgqf.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.xdjsx.cn.gov.cn.xdjsx.cn http://www.morning.fssjw.cn.gov.cn.fssjw.cn http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn http://www.morning.phwmj.cn.gov.cn.phwmj.cn http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn http://www.morning.tnhqr.cn.gov.cn.tnhqr.cn