// 函数组件使用forwardRef传递ref
const ForwardRefComponent = React.forwardRef((props, ref) => this)} {...props}>子组件DOM
)
export default function TestRef() {
let myRef = null;
return (
<>
(myRef = r)} />
>
)
}
// Class组件使用forwardRef传递ref
class Child extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
this.props.forwardedRef}>这是子组件DOM
)
}
}
const wrapper = function (InnerComponent) {
return React.forwardRef((props, ref) => {
return (
)
})
}
const W = wrapper(Child)
class Parent extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
render() {
return (
this.myRef} { ...this.props}/>
)
}
}