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

Fix "null" instead of the component stack in a warning #10915

Merged
merged 4 commits into from
Sep 28, 2017
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
37 changes: 37 additions & 0 deletions src/renderers/__tests__/ReactStatelessComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,43 @@ describe('ReactStatelessComponent', () => {
console.error.calls.reset();
});

// This guards against a regression caused by clearing the current debug fiber.
// https://github.com/facebook/react/issues/10831
it('should warn when giving a function ref with context', () => {
spyOn(console, 'error');

function Child() {
return null;
}
Child.contextTypes = {
foo: PropTypes.string,
};

class Parent extends React.Component {
static childContextTypes = {
foo: PropTypes.string,
};
getChildContext() {
return {
foo: 'bar',
};
}
render() {
return <Child ref={function() {}} />;
}
}

ReactTestUtils.renderIntoDocument(<Parent />);
expectDev(console.error.calls.count()).toBe(1);
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Stateless function components cannot be given refs. ' +
'Attempts to access this ref will fail.\n\nCheck the render method ' +
'of `Parent`.\n' +
' in Child (at **)\n' +
' in Parent (at **)',
);
});

it('should provide a null ref', () => {
function Child() {
return <div />;
Expand Down
7 changes: 6 additions & 1 deletion src/renderers/shared/fiber/ReactDebugCurrentFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ function resetCurrentFiber() {
ReactDebugCurrentFiber.phase = null;
}

function setCurrentFiber(fiber: Fiber | null, phase: LifeCyclePhase | null) {
function setCurrentFiber(fiber: Fiber) {
ReactDebugCurrentFrame.getCurrentStack = getCurrentFiberStackAddendum;
ReactDebugCurrentFiber.current = fiber;
ReactDebugCurrentFiber.phase = null;
}

function setCurrentPhase(phase: LifeCyclePhase | null) {
ReactDebugCurrentFiber.phase = phase;
}

Expand All @@ -66,6 +70,7 @@ var ReactDebugCurrentFiber = {
phase: (null: LifeCyclePhase | null),
resetCurrentFiber,
setCurrentFiber,
setCurrentPhase,
getCurrentFiberOwnerName,
getCurrentFiberStackAddendum,
};
Expand Down
12 changes: 4 additions & 8 deletions src/renderers/shared/fiber/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(

if (__DEV__) {
ReactCurrentOwner.current = workInProgress;
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, 'render');
ReactDebugCurrentFiber.setCurrentPhase('render');
nextChildren = fn(nextProps, context);
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
ReactDebugCurrentFiber.setCurrentPhase(null);
} else {
nextChildren = fn(nextProps, context);
}
Expand Down Expand Up @@ -281,9 +281,9 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
ReactCurrentOwner.current = workInProgress;
let nextChildren;
if (__DEV__) {
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, 'render');
ReactDebugCurrentFiber.setCurrentPhase('render');
nextChildren = instance.render();
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
ReactDebugCurrentFiber.setCurrentPhase(null);
} else {
nextChildren = instance.render();
}
Expand Down Expand Up @@ -725,10 +725,6 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
return bailoutOnLowPriority(current, workInProgress);
}

if (__DEV__) {
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
}

switch (workInProgress.tag) {
case IndeterminateComponent:
return mountIndeterminateComponent(
Expand Down
8 changes: 0 additions & 8 deletions src/renderers/shared/fiber/ReactFiberCompleteWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ var {
var {Placement, Ref, Update} = ReactTypeOfSideEffect;
var {OffscreenPriority} = ReactPriorityLevel;

if (__DEV__) {
var ReactDebugCurrentFiber = require('ReactDebugCurrentFiber');
}

var invariant = require('fbjs/lib/invariant');

module.exports = function<T, P, I, TI, PI, C, CX, PL>(
Expand Down Expand Up @@ -187,10 +183,6 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
workInProgress: Fiber,
renderPriority: PriorityLevel,
): Fiber | null {
if (__DEV__) {
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
}

// Get the latest props.
let newProps = workInProgress.pendingProps;
if (newProps === null) {
Expand Down
31 changes: 9 additions & 22 deletions src/renderers/shared/fiber/ReactFiberContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ exports.getMaskedContext = function(

if (__DEV__) {
const name = getComponentName(workInProgress) || 'Unknown';
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
checkPropTypes(
contextTypes,
context,
'context',
name,
ReactDebugCurrentFiber.getCurrentFiberStackAddendum,
);
ReactDebugCurrentFiber.resetCurrentFiber();
}

// Cache unmasked context so we can avoid recreating masked context unless necessary.
Expand Down Expand Up @@ -153,11 +151,7 @@ exports.pushTopLevelContextObject = function(
push(didPerformWorkStackCursor, didChange, fiber);
};

function processChildContext(
fiber: Fiber,
parentContext: Object,
isReconciling: boolean,
): Object {
function processChildContext(fiber: Fiber, parentContext: Object): Object {
const instance = fiber.stateNode;
const childContextTypes = fiber.type.childContextTypes;

Expand All @@ -184,11 +178,11 @@ function processChildContext(

let childContext;
if (__DEV__) {
ReactDebugCurrentFiber.setCurrentFiber(fiber, 'getChildContext');
ReactDebugCurrentFiber.setCurrentPhase('getChildContext');
startPhaseTimer(fiber, 'getChildContext');
childContext = instance.getChildContext();
stopPhaseTimer();
ReactDebugCurrentFiber.resetCurrentFiber();
ReactDebugCurrentFiber.setCurrentPhase(null);
} else {
childContext = instance.getChildContext();
}
Expand All @@ -202,21 +196,18 @@ function processChildContext(
}
if (__DEV__) {
const name = getComponentName(fiber) || 'Unknown';
// We can only provide accurate element stacks if we pass work-in-progress tree
// during the begin or complete phase. However currently this function is also
// called from unstable_renderSubtree legacy implementation. In this case it unsafe to
// assume anything about the given fiber. We won't pass it down if we aren't sure.
// TODO: remove this hack when we delete unstable_renderSubtree in Fiber.
const workInProgress = isReconciling ? fiber : null;
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
checkPropTypes(
childContextTypes,
childContext,
'child context',
name,
// In practice, there is one case in which we won't get a stack. It's when
// somebody calls unstable_renderSubtreeIntoContainer() and we process
// context from the parent component instance. The stack will be missing
// because it's outside of the reconciliation, and so the pointer has not
// been set. This is rare and doesn't matter. We'll also remove that API.
ReactDebugCurrentFiber.getCurrentFiberStackAddendum,
);
ReactDebugCurrentFiber.resetCurrentFiber();
}

return {...parentContext, ...childContext};
Expand Down Expand Up @@ -264,11 +255,7 @@ exports.invalidateContextProvider = function(
// Merge parent and own context.
// Skip this if we're not updating due to sCU.
// This avoids unnecessarily recomputing memoized values.
const mergedContext = processChildContext(
workInProgress,
previousContext,
true,
);
const mergedContext = processChildContext(workInProgress, previousContext);
instance.__reactInternalMemoizedMergedChildContext = mergedContext;

// Replace the old (or empty) context with the new one.
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/shared/fiber/ReactFiberReconciler.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export type Reconciler<C, I, TI> = {
getContextForSubtree._injectFiber(function(fiber: Fiber) {
const parentContext = findCurrentUnmaskedContext(fiber);
return isContextProvider(fiber)
? processChildContext(fiber, parentContext, false)
? processChildContext(fiber, parentContext)
: parentContext;
});

Expand Down
22 changes: 15 additions & 7 deletions src/renderers/shared/fiber/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
function commitAllHostEffects() {
while (nextEffect !== null) {
if (__DEV__) {
ReactDebugCurrentFiber.setCurrentFiber(nextEffect, null);
ReactDebugCurrentFiber.setCurrentFiber(nextEffect);
recordEffect();
}

Expand Down Expand Up @@ -615,7 +615,13 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
// means that we don't need an additional field on the work in
// progress.
const current = workInProgress.alternate;
if (__DEV__) {
ReactDebugCurrentFiber.setCurrentFiber(workInProgress);
}
const next = completeWork(current, workInProgress, nextPriorityLevel);
if (__DEV__) {
ReactDebugCurrentFiber.resetCurrentFiber();
}

const returnFiber = workInProgress.return;
const siblingFiber = workInProgress.sibling;
Expand Down Expand Up @@ -706,8 +712,12 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
// See if beginning this work spawns more work.
if (__DEV__) {
startWorkTimer(workInProgress);
ReactDebugCurrentFiber.setCurrentFiber(workInProgress);
}
let next = beginWork(current, workInProgress, nextPriorityLevel);
if (__DEV__) {
ReactDebugCurrentFiber.resetCurrentFiber();
}
if (__DEV__ && ReactFiberInstrumentation.debugTool) {
ReactFiberInstrumentation.debugTool.onBeginWork(workInProgress);
}
Expand All @@ -718,9 +728,6 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
}

ReactCurrentOwner.current = null;
if (__DEV__) {
ReactDebugCurrentFiber.resetCurrentFiber();
}

return next;
}
Expand All @@ -735,8 +742,12 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
// See if beginning this work spawns more work.
if (__DEV__) {
startWorkTimer(workInProgress);
ReactDebugCurrentFiber.setCurrentFiber(workInProgress);
}
let next = beginFailedWork(current, workInProgress, nextPriorityLevel);
if (__DEV__) {
ReactDebugCurrentFiber.resetCurrentFiber();
}
if (__DEV__ && ReactFiberInstrumentation.debugTool) {
ReactFiberInstrumentation.debugTool.onBeginWork(workInProgress);
}
Expand All @@ -747,9 +758,6 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
}

ReactCurrentOwner.current = null;
if (__DEV__) {
ReactDebugCurrentFiber.resetCurrentFiber();
}

return next;
}
Expand Down