forked from facebook/react
/
ReactDebugCurrentFrame.js
60 lines (49 loc) · 1.64 KB
/
ReactDebugCurrentFrame.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
/**
* 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
*/
import type {ReactElement} from 'shared/ReactElementType';
import describeComponentFrame from 'shared/describeComponentFrame';
import getComponentName from 'shared/getComponentName';
const ReactDebugCurrentFrame = {};
let currentlyValidatingElement = (null: null | ReactElement);
export function setCurrentlyValidatingElement(element: null | ReactElement) {
if (__DEV__) {
currentlyValidatingElement = element;
}
}
if (__DEV__) {
ReactDebugCurrentFrame.setCurrentlyValidatingElement = function(
element: null | ReactElement,
) {
if (__DEV__) {
currentlyValidatingElement = element;
}
};
// Stack implementation injected by the current renderer.
ReactDebugCurrentFrame.getCurrentStack = (null: null | (() => string));
ReactDebugCurrentFrame.getStackAddendum = function(): string {
let stack = '';
// Add an extra top frame while an element is being validated
if (currentlyValidatingElement) {
const name = getComponentName(currentlyValidatingElement.type);
const owner = currentlyValidatingElement._owner;
stack += describeComponentFrame(
name,
currentlyValidatingElement._source,
owner && getComponentName(owner.type),
);
}
// Delegate to the injected renderer-specific implementation
const impl = ReactDebugCurrentFrame.getCurrentStack;
if (impl) {
stack += impl() || '';
}
return stack;
};
}
export default ReactDebugCurrentFrame;