1 | import { extendComponent } from './extendComponent';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | export function initializeComponentRef(obj) {
|
9 | extendComponent(obj, {
|
10 | componentDidMount: _onMount,
|
11 | componentDidUpdate: _onUpdate,
|
12 | componentWillUnmount: _onUnmount,
|
13 | });
|
14 | }
|
15 | function _onMount() {
|
16 | _setComponentRef(this.props.componentRef, this);
|
17 | }
|
18 | function _onUpdate(prevProps) {
|
19 | if (prevProps.componentRef !== this.props.componentRef) {
|
20 |
|
21 | _setComponentRef(prevProps.componentRef, null);
|
22 | _setComponentRef(this.props.componentRef, this);
|
23 | }
|
24 | }
|
25 | function _onUnmount() {
|
26 | _setComponentRef(this.props.componentRef, null);
|
27 | }
|
28 | function _setComponentRef(componentRef, value) {
|
29 | if (componentRef) {
|
30 | if (typeof componentRef === 'object') {
|
31 | componentRef.current = value;
|
32 | }
|
33 | else if (typeof componentRef === 'function') {
|
34 | componentRef(value);
|
35 | }
|
36 | }
|
37 | }
|
38 |
|
\ | No newline at end of file |