react的转发ref
乔文飞 Lv8

简单理解

使用通用的高阶组件时,外层组件ref控制高阶组件中包裹的子组件中的元素,此时外层组件传入的ref并不作用于高阶上,这个时候,高阶组件就要用到React.forwardRef进行ref转发。

官方文档

React.forwardRef

简单举栗🌰

🌰栗子 来自JamesSawyer—-React 中的转发ref

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  // 高阶组件
import React from 'react';

function logProps(Component) {
class LogProps extends React.Component {
componentDidUpdate(prevProps) {
console.log('先前的属性:', prevProps);
console.log('当前属性:', this.props);
}

render() {
// 使用forwardedRef作为一个ref属性传入组件中
const { forwardedRef, ...rest } = this.props;
return (
<Component ref={forwardedRef} {...rest} />
);
}
}

// 使用React.forwardRef对LogProps组件进行转发
return React.forwardRef((props, ref) => (
{' 上面定义的LogProps组件接受一个forwarded属性 '}
<LogProps forwardedRef={ref} {...props} />
));
}

// FancyButton.js 子组件
import React from 'react';
import logProps from './logProps';

// 接受props和ref作为参数
// 返回一个React 组件
const FancyButton = React.forwardRef((props, ref) => (
<button class="fancybutton" ref={ref}>
{props.children}
</button>
));

// 使用高阶组件对其进行封装
export default logProps(FancyButton);

// 父组件
// app.js
class App extends React.Component {

constructor(props) {
super(props);
// 创建一个ref 名字随意
this.ref = React.createRef();
}

componentDidMount() {
console.log('ref', this.ref);
// this.ref.current 表示获取ref指向的DOM元素
this.ref.current.classList.add('primary'); // 给FancyButton中的button添加一个class
this.ref.current.focus(); // focus到button元素上
}

render() {
// 直接使用ref={this.fancyButtonRef}
return (
<FancyButton ref={this.fancyButtonRef}>子组件</FancyButton>
);
}
}
  • 本文标题:react的转发ref
  • 本文作者:乔文飞
  • 创建时间:2021-07-12 17:18:41
  • 本文链接:http://www.feidom.com/2021/07/12/react的转发ref/
  • 版权声明:本博客所有文章为作者学习笔记,有转载其他前端大佬的文章。转载时请注明出处。