UNPKG

1.24 kBJavaScriptView Raw
1import { extendComponent } from './extendComponent';
2/**
3 * Helper to manage componentRef resolution. Internally appends logic to
4 * lifetime methods to resolve componentRef to the passed in object.
5 *
6 * Usage: call initializeComponentRef(this) in the constructor,
7 */
8export function initializeComponentRef(obj) {
9 extendComponent(obj, {
10 componentDidMount: _onMount,
11 componentDidUpdate: _onUpdate,
12 componentWillUnmount: _onUnmount,
13 });
14}
15function _onMount() {
16 _setComponentRef(this.props.componentRef, this);
17}
18function _onUpdate(prevProps) {
19 if (prevProps.componentRef !== this.props.componentRef) {
20 // eslint-disable-next-line @typescript-eslint/no-explicit-any
21 _setComponentRef(prevProps.componentRef, null);
22 _setComponentRef(this.props.componentRef, this);
23 }
24}
25function _onUnmount() {
26 _setComponentRef(this.props.componentRef, null);
27}
28function _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//# sourceMappingURL=initializeComponentRef.js.map
\No newline at end of file