-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrackedViews.js
56 lines (48 loc) · 1.44 KB
/
TrackedViews.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* @flow
*/
import React, {
Component
} from 'react';
import {
Text,
View,
} from 'react-native';
import {registerView, unregisterView} from './ViewTracker'
/**
* Higher-order component that turns a built-in View component into a component
* that is tracked with the key specified in the viewKey prop. If the viewKey
* prop is not specified, then no tracking is done.
*/
const createTrackedView = (ViewComponent: any) => class TrackedComponent extends Component {
componentDidMount() {
const {viewKey} = this.props;
if (viewKey) {
registerView(viewKey, this.refs.viewRef);
}
}
componentWillUpdate(nextProps: any) {
const {viewKey} = this.props;
if (viewKey && !viewKey.equals(nextProps.viewKey)) {
unregisterView(viewKey, this.refs.viewRef);
}
}
componentDidUpdate(prevProps: any) {
const {viewKey} = this.props;
if (viewKey && !viewKey.equals(prevProps.viewKey)) {
registerView(viewKey, this.refs.viewRef);
}
}
componentWillUnmount() {
const {viewKey} = this.props;
if (viewKey) {
unregisterView(viewKey, this.refs.viewRef);
}
}
render() {
const {viewKey, ...childProps} = this.props;
return <ViewComponent ref="viewRef" {...childProps} />
}
};
export const TrackedView = createTrackedView(View);
export const TrackedText = createTrackedView(Text);