React

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > React > React forwardRef和useImperativeHandle

React中forwardRef和useImperativeHandle的实现

作者:JJCTO袁龙

本文主要介绍了React中forwardRef和useImperativeHandle的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一句话总结:forwardRef 让父组件拿到子组件的 DOM 节点,useImperativeHandle 让子组件只暴露「指定的方法」给父组件调用。一个是透传 ref,一个自定义 ref 内容,配合使用精确控制组件对外暴露的能力!

一、forwardRef 和 useImperativeHandle 是什么?

问题背景:React 函数组件没有 this,父组件无法直接拿到子组件的实例或 DOM。

// ❌ 函数组件无法接收 ref
function Input() {
  return <input />;
}

function Parent() {
  const inputRef = useRef();
  return <Input ref={inputRef} />;   // ❌ ref 传不进函数组件
  // inputRef.current 是 null
}

forwardRef:让函数组件能接收 ref 并转发到内部 DOM

// ✅ 用 forwardRef 包裹,接收 ref 参数
const Input = forwardRef((props, ref) => {
  return <input ref={ref} />;   // ✅ ref 转发给 input DOM
});

function Parent() {
  const inputRef = useRef();
  useEffect(() => {
    inputRef.current?.focus();   // ✅ 拿到 input DOM
  }, []);
  return <Input ref={inputRef} />;
}

useImperativeHandle:自定义 ref.current 的内容,不直接暴露 DOM

// ✅ 用 useImperativeHandle 只暴露指定方法
const Input = forwardRef((props, ref) => {
  const inputRef = useRef();

  useImperativeHandle(ref, () => ({
    focus: () => inputRef.current?.focus(),
    clear: () => { inputRef.current.value = ''; },
    getValue: () => inputRef.current.value,
  }));   // ✅ ref.current = { focus, clear, getValue }

  return <input ref={inputRef} />;
});

function Parent() {
  const inputRef = useRef();
  // inputRef.current.focus()  → 聚焦
  // inputRef.current.clear()  → 清空
  // inputRef.current.getValue() → 获取值
  // ❌ inputRef.current 不再是 DOM,拿不到 inputRef.current.value
  return <Input ref={inputRef} />;
}
对比项forwardRefuseImperativeHandle
作用透传 ref 到 DOM自定义 ref.current 内容
ref.current 是什么DOM 节点你定义的对象
对外暴露整个 DOM只暴露指定方法
使用场景需要直接操作 DOM封装组件 API
配合使用可单独用必须配合 forwardRef

二、5 大高频场景 & 代码示例

① 表单组件:父组件需要聚焦子组件的 input

// ❌ 父组件想自动聚焦,但拿不到子组件的 input
function MyInput(props) {
  return <input {...props} />;
}

function Form() {
  const ref = useRef();
  useEffect(() => {
    ref.current?.focus();   // ❌ ref.current 是 null
  }, []);
  return <MyInput ref={ref} />;   // ❌ 函数组件不接收 ref
}

修复:用 forwardRef 透传

// ✅ forwardRef 透传 ref 到内部 input
const MyInput = forwardRef((props, ref) => {
  return <input ref={ref} {...props} />;
});

function Form() {
  const inputRef = useRef();
  useEffect(() => {
    inputRef.current?.focus();   // ✅ 拿到 input DOM,自动聚焦
  }, []);
  return <MyInput ref={inputRef} />;
}

② 高阶组件:透传 ref 但 ref 被吞了

// ❌ 高阶组件吞掉了 ref
function withLogger(WrappedComponent) {
  function Hoc(props) {
    useEffect(() => {
      console.log('props changed', props);
    });
    return <WrappedComponent {...props} />;   // ❌ ref 没传进去
  }
  return Hoc;
}

const EnhancedInput = withLogger(MyInput);

function Parent() {
  const ref = useRef();
  return <EnhancedInput ref={ref} />;   // ❌ ref.current 是 null
}

修复:用 forwardRef 透传 ref

// ✅ forwardRef 透传 ref
function withLogger(WrappedComponent) {
  const Hoc = forwardRef((props, ref) => {
    useEffect(() => {
      console.log('props changed', props);
    });
    return <WrappedComponent ref={ref} {...props} />;   // ✅ 透传 ref
  });
  return Hoc;
}

③ 封装组件:不想暴露整个 DOM,只暴露方法

// ❌ 直接暴露 DOM,父组件能随意操作内部结构
const MyInput = forwardRef((props, ref) => {
  return <input ref={ref} {...props} />;
});

function Parent() {
  const ref = useRef();
  // ❌ ref.current 是 input DOM,可以随意改 style、value 等
  // 违反了组件封装原则
  return <MyInput ref={ref} />;
}

修复:用 useImperativeHandle 只暴露需要的方法

import { forwardRef, useRef, useImperativeHandle } from 'react';

// ✅ 只暴露 focus 和 clear,不暴露 DOM
const MyInput = forwardRef((props, ref) => {
  const inputRef = useRef();

  useImperativeHandle(ref, () => ({
    focus: () => inputRef.current?.focus(),
    clear: () => { if (inputRef.current) inputRef.current.value = ''; },
    getValue: () => inputRef.current?.value || '',
    setValue: (val) => { if (inputRef.current) inputRef.current.value = val; },
  }), []);   // ✅ 依赖空数组:方法引用稳定

  return <input ref={inputRef} {...props} />;
});

function Parent() {
  const inputRef = useRef();
  // ✅ 只能用 focus/clear/getValue/setValue,碰不到 DOM
  return (
    <>
      <button onClick={() => inputRef.current?.focus()}>聚焦</button>
      <button onClick={() => inputRef.current?.clear()}>清空</button>
      <button onClick={() => console.log(inputRef.current?.getValue())}>取值</button>
      <MyInput ref={inputRef} />
    </>
  );
}

④ useImperativeHandle 的依赖没写对,方法引用不稳定

// ❌ 没写依赖数组,每次渲染都创建新对象 → 父组件 memo 子组件失效
const MyInput = forwardRef((props, ref) => {
  const inputRef = useRef();

  useImperativeHandle(ref, () => ({
    focus: () => inputRef.current?.focus(),
    validate: () => props.validator(inputRef.current?.value),
  }));   // ❌ 没有依赖数组 → 每次渲染创建新对象

  return <input ref={inputRef} />;
});

修复:加依赖数组

// ✅ 加依赖数组,引用稳定
const MyInput = forwardRef((props, ref) => {
  const inputRef = useRef();

  useImperativeHandle(ref, () => ({
    focus: () => inputRef.current?.focus(),
    validate: () => props.validator(inputRef.current?.value),
  }), [props.validator]);   // ✅ validator 变化时才重建

  return <input ref={inputRef} />;
});

⑤ 子组件暴露方法配合父组件做表单校验

// ✅ 子组件暴露校验方法,父组件统一收集
const Field = forwardRef(({ label, rules = [] }, ref) => {
  const inputRef = useRef();
  const [error, setError] = useState('');

  useImperativeHandle(ref, () => ({
    validate: () => {
      const value = inputRef.current?.value || '';
      for (const rule of rules) {
        if (rule.required && !value) {
          setError(rule.message || '必填');
          return false;
        }
        if (rule.min && value.length < rule.min) {
          setError(rule.message || `最少${rule.min}个字符`);
          return false;
        }
      }
      setError('');
      return true;
    },
    getValue: () => inputRef.current?.value || '',
  }), [rules]);

  return (
    <div>
      <label>{label}</label>
      <input ref={inputRef} />
      {error && <span className="error">{error}</span>}
    </div>
  );
});

// ✅ 父组件统一校验
function Form() {
  const nameRef = useRef();
  const emailRef = useRef();

  const handleSubmit = () => {
    const validations = [nameRef, emailRef];
    const allValid = validations.every(ref => ref.current?.validate());
    if (allValid) {
      const data = {
        name: nameRef.current?.getValue(),
        email: emailRef.current?.getValue(),
      };
      console.log('提交', data);
    }
  };

  return (
    <form onSubmit={handleSubmit}>
      <Field ref={nameRef} label="姓名" rules={[{ required: true, message: '请输入姓名' }]} />
      <Field ref={emailRef} label="邮箱" rules={[{ required: true, message: '请输入邮箱' }]} />
      <button type="submit">提交</button>
    </form>
  );
}

三、万能兜底:ref 暴露决策表

需求用什么示例
父组件需要操作子组件 DOMforwardRefinputRef.current.focus()
父组件只需调子组件方法forwardRef + useImperativeHandleref.current.validate()
高阶组件透传 refforwardRef 包裹 HOCforwardRef((props, ref) => <Wrapped ref={ref} />)
不想暴露 DOM 只暴露方法useImperativeHandle{ focus, clear, validate }
多个子组件统一管理ref 数组 + useImperativeHandlerefs.forEach(r => r.validate())

万能模板(封装组件标准写法):

import { forwardRef, useRef, useImperativeHandle } from 'react';

// ✅ 1. 用 forwardRef 包裹
const CustomInput = forwardRef(({ label, rules, ...props }, ref) => {
  const inputRef = useRef();
  const [error, setError] = useState('');

  // ✅ 2. 用 useImperativeHandle 暴露 API
  useImperativeHandle(ref, () => ({
    // 暴露方法
    focus: () => inputRef.current?.focus(),
    blur: () => inputRef.current?.blur(),
    clear: () => { if (inputRef.current) inputRef.current.value = ''; },
    getValue: () => inputRef.current?.value || '',
    setValue: (val) => { if (inputRef.current) inputRef.current.value = val; },
    validate: () => {
      const value = inputRef.current?.value || '';
      for (const rule of rules || []) {
        if (!rule.validate(value)) {
          setError(rule.message);
          return false;
        }
      }
      setError('');
      return true;
    },
    getError: () => error,
  }), [rules, error]);   // ✅ 3. 依赖数组

  return (
    <div className="custom-input">
      {label && <label>{label}</label>}
      <input ref={inputRef} {...props} />
      {error && <span className="error-message">{error}</span>}
    </div>
  );
});

// ✅ 4. 定义类型(TypeScript)
CustomInput.displayName = 'CustomInput';

export default CustomInput;

四、预防 checklist

五、一句话总结

「forwardRef 透传 ref 到 DOM,useImperativeHandle 自定义 ref 内容只暴露方法」。需要操作 DOM 用 forwardRef,需要封装组件 API 用 useImperativeHandle。两者配合,精确控制组件对外暴露的能力,封装性和可维护性拉满!

到此这篇关于React中forwardRef和useImperativeHandle的实现的文章就介绍到这了,更多相关React forwardRef和useImperativeHandle内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文