Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose component stack from reactTag to React Native renderer #12549

Merged
merged 1 commit into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/react-native-renderer/src/ReactNativeRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import ReactVersion from 'shared/ReactVersion';
// Module provided by RN:
import UIManager from 'UIManager';

import {getStackAddendumByWorkInProgressFiber} from 'shared/ReactFiberComponentTreeHook';

import NativeMethodsMixin from './NativeMethodsMixin';
import ReactNativeBridgeEventPlugin from './ReactNativeBridgeEventPlugin';
import ReactNativeComponent from './ReactNativeComponent';
Expand All @@ -35,6 +37,14 @@ injectFindHostInstance(ReactNativeFiberRenderer.findHostInstance);

ReactGenericBatching.injection.injectRenderer(ReactNativeFiberRenderer);

function computeComponentStackForErrorReporting(reactTag: number): string {
let fiber = ReactNativeComponentTree.getClosestInstanceFromNode(reactTag);
if (!fiber) {
return '';
}
return getStackAddendumByWorkInProgressFiber(fiber);
}

const roots = new Map();

const ReactNativeRenderer: ReactNativeType = {
Expand Down Expand Up @@ -99,6 +109,7 @@ const ReactNativeRenderer: ReactNativeType = {
TouchHistoryMath, // PanResponder
createReactNativeComponentClass, // RCTText, RCTView, ReactNativeART
takeSnapshot, // react-native-implementation
computeComponentStackForErrorReporting,
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
* @jest-environment node
*/

'use strict';

let React;
let ReactNative;
let createReactNativeComponentClass;
let computeComponentStackForErrorReporting;

function normalizeCodeLocInfo(str) {
return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
}

describe('ReactNativeError', () => {
beforeEach(() => {
jest.resetModules();

React = require('react');
ReactNative = require('react-native-renderer');
createReactNativeComponentClass = require('../createReactNativeComponentClass')
.default;
computeComponentStackForErrorReporting =
ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.computeComponentStackForErrorReporting;
});

it('should be able to extract a component stack from a native view', () => {
const View = createReactNativeComponentClass('View', () => ({
validAttributes: {foo: true},
uiViewClassName: 'View',
}));

const ref = React.createRef();

function FunctionalComponent(props) {
return props.children;
}

class ClassComponent extends React.Component {
render() {
return (
<FunctionalComponent>
<View foo="test" ref={ref} />
</FunctionalComponent>
);
}
}

ReactNative.render(<ClassComponent />, 1);

let reactTag = ReactNative.findNodeHandle(ref.current);

let componentStack = normalizeCodeLocInfo(
computeComponentStackForErrorReporting(reactTag),
);

if (__DEV__) {
expect(componentStack).toBe(
'\n' +
' in View (at **)\n' +
' in FunctionalComponent (at **)\n' +
' in ClassComponent (at **)',
);
} else {
expect(componentStack).toBe(
'\n' +
' in View\n' +
' in FunctionalComponent\n' +
' in ClassComponent',
);
}
});
});