# forwardRef 用法

# 1.暴露句柄

import { useRef } from 'react';
import Child from './components/Child'
import { Button } from 'antd';

export default () => {
  const childRef = useRef(null);
  const hanldeClick = () => {
    childRef?.current?.setChildName('陈陈' + new Date().getTime());
  }
  return (
    <div>
      父组件
      <Button onClick={hanldeClick}>改名字</Button>
      <Child ref={childRef} />
    </div>
  );
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import React, { useState, forwardRef, useImperativeHandle } from 'react';

const Child = (props: any, ref) => {
    const [name, setName] = useState('');
    useImperativeHandle(ref, () => ({
        setChildName: (val: any) => {
            setName(val);
        },
    }));

    return <div>
        这是儿子的名字{name}
    </div>
}

export default forwardRef(Child);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 2.暴露DOM节点

import { useRef } from 'react';
import Child from './components/Child'
import { Button } from 'antd';

export default () => {
  const childRef = useRef(null);
  const hanldeClick = () => {
    childRef?.current?.focus();
  }
  return (
    <div>
      父组件
      <Button onClick={hanldeClick}>聚集</Button>
      <Child ref={childRef} />
    </div>
  );
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import React, { forwardRef } from 'react';

const Child = (props: any, ref) => {
    return <div>
        <input ref={ref} />
    </div>
}

export default forwardRef(Child);
1
2
3
4
5
6
7
8
9

# 在组件上下文中层层转发

forwardRef文档参考链接

最后更新时间: 7/2/2025, 4:11:33 PM