mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
We currently use the stack to dedupe warnings in a couple of places. This is a very heavy weight way of computing that a warning doesn't need to be fired. This uses parent component name as a heuristic for deduping. It's not perfect but as soon as you fix one you'll uncover the next. It might be a little annoying but having many logs is also annoying. We now have no special cases for stacks. The only thing that uses stacks in dev is the console.error and dev tools. This means that we could externalize this completely to an console.error patching module and drop it from being built-in to react. The only prod/dev behavior is the one we pass to error boundaries or the error we throw if you don't have an error boundary.
73 lines
1.7 KiB
JavaScript
73 lines
1.7 KiB
JavaScript
/**
|
|
* 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 {Fiber} from './ReactInternalTypes';
|
|
|
|
import ReactSharedInternals from 'shared/ReactSharedInternals';
|
|
import {getStackByFiberInDevAndProd} from './ReactFiberComponentStack';
|
|
import getComponentName from 'shared/getComponentName';
|
|
|
|
const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
|
|
|
|
export let current: Fiber | null = null;
|
|
export let isRendering: boolean = false;
|
|
|
|
export function getCurrentFiberOwnerNameInDevOrNull(): string | null {
|
|
if (__DEV__) {
|
|
if (current === null) {
|
|
return null;
|
|
}
|
|
const owner = current._debugOwner;
|
|
if (owner !== null && typeof owner !== 'undefined') {
|
|
return getComponentName(owner.type);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function getCurrentFiberStackInDev(): string {
|
|
if (__DEV__) {
|
|
if (current === null) {
|
|
return '';
|
|
}
|
|
// Safe because if current fiber exists, we are reconciling,
|
|
// and it is guaranteed to be the work-in-progress version.
|
|
return getStackByFiberInDevAndProd(current);
|
|
}
|
|
return '';
|
|
}
|
|
|
|
export function resetCurrentFiber() {
|
|
if (__DEV__) {
|
|
ReactDebugCurrentFrame.getCurrentStack = null;
|
|
current = null;
|
|
isRendering = false;
|
|
}
|
|
}
|
|
|
|
export function setCurrentFiber(fiber: Fiber) {
|
|
if (__DEV__) {
|
|
ReactDebugCurrentFrame.getCurrentStack = getCurrentFiberStackInDev;
|
|
current = fiber;
|
|
isRendering = false;
|
|
}
|
|
}
|
|
|
|
export function setIsRendering(rendering: boolean) {
|
|
if (__DEV__) {
|
|
isRendering = rendering;
|
|
}
|
|
}
|
|
|
|
export function getIsRendering() {
|
|
if (__DEV__) {
|
|
return isRendering;
|
|
}
|
|
}
|