-
Notifications
You must be signed in to change notification settings - Fork 45.6k
/
NativeMethodsMixinUtils.js
84 lines (77 loc) · 2.55 KB
/
NativeMethodsMixinUtils.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
/**
* In the future, we should cleanup callbacks by cancelling them instead of
* using this.
*/
export function mountSafeCallback_NOT_REALLY_SAFE(
context: any,
callback: ?Function,
): any {
return function() {
if (!callback) {
return undefined;
}
// This protects against createClass() components.
// We don't know if there is code depending on it.
// We intentionally don't use isMounted() because even accessing
// isMounted property on a React ES6 class will trigger a warning.
if (typeof context.__isMounted === 'boolean') {
if (!context.__isMounted) {
return undefined;
}
}
// FIXME: there used to be other branches that protected
// against unmounted host components. But RN host components don't
// define isMounted() anymore, so those checks didn't do anything.
// They caused false positive warning noise so we removed them:
// https://github.com/facebook/react-native/issues/18868#issuecomment-413579095
// However, this means that the callback is NOT guaranteed to be safe
// for host components. The solution we should implement is to make
// UIManager.measure() and similar calls truly cancelable. Then we
// can change our own code calling them to cancel when something unmounts.
return callback.apply(context, arguments);
};
}
export function throwOnStylesProp(component: any, props: any) {
if (props.styles !== undefined) {
const owner = component._owner || null;
const name = component.constructor.displayName;
let msg =
'`styles` is not a supported property of `' +
name +
'`, did ' +
'you mean `style` (singular)?';
if (owner && owner.constructor && owner.constructor.displayName) {
msg +=
'\n\nCheck the `' +
owner.constructor.displayName +
'` parent ' +
' component.';
}
throw new Error(msg);
}
}
export function warnForStyleProps(props: any, validAttributes: any) {
if (__DEV__) {
for (const key in validAttributes.style) {
if (!(validAttributes[key] || props[key] === undefined)) {
console.error(
'You are setting the style `{ %s' +
': ... }` as a prop. You ' +
'should nest it in a style object. ' +
'E.g. `{ style: { %s' +
': ... } }`',
key,
key,
);
}
}
}
}