**例子**
```
import React from "react";
const TargetComponet = React.forwardRef((props,ref ) =>(
<input type="text"
ref={ ref }
/>
));
export default class Comp extends React.Component{
constructor(props) {
super(props);
this.ref = React.createRef();
}
componentDidMount() {
this.ref.current.value = 'ref get input'
}
render() {
return <TargetComponet ref={this.ref} />
}
}
```
**源碼**
~~~
import {REACT_FORWARD_REF_TYPE, REACT_MEMO_TYPE} from 'shared/ReactSymbols';
export default function forwardRef<Props, ElementType: React$ElementType>(
render: (props: Props, ref: React$Ref<ElementType>) => React$Node,
) {
return {
$$typeof: REACT_FORWARD_REF_TYPE,
render,
};
}
~~~
> 通過React.ReactElement創建的所有節點都是 REACT_ELEMENT_TYPE
- 說明
- react源碼
- 問答
- 慕課網視頻
- 第二章:基礎知識
- 001.ReactElement
- 002.react-component
- 003.ref
- 004.forwardRef
- 005.context
- 006.concurrentMode
- 007.supense和lazy
- 008.hooks
- 009.children
- 010.memo
- 011.others
- 第三章:react的更新
- 001.react-dom-render
- 第四章:Fiber Scheduler
- 第五章:各類組件的Update
- 第六章:完成節點任務
- 第七章:commitRoot
- 第八章:功能詳解:基礎
- 第九章:suspense and priority
- 第十章:功能詳解:Hooks
- 001.基礎知識
- 002.hook
- 003.RootFiber
- 004. hydrate
- react
- 高階組件
- react基礎
- Github面試題目