ref 指向对象本身
React.forwardRef 转发ref属性:通过组件向子组件自动引用ref。
如:
import React, { Component, createRef, forwardRef } from 'react';
const FocusInput = forwardRef((props, ref) => (
<input type="text" ref={ref} />
));
class ForwardRef extends Component {
constructor(props) {
super(props);
this.ref = createRef();
}
componentDidMount() {
const { current } = this.ref;
current.focus();
}
render() {
return (
<div>
<p>forward ref</p>
<FocusInput ref={this.ref} />
</div>
);
}
}
export default ForwardRef;