Skip to content

Commit

Permalink
Use new error message in fiber
Browse files Browse the repository at this point in the history
  • Loading branch information
davidblurton committed Mar 2, 2017
1 parent 7f56520 commit f85b091
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/renderers/dom/fiber/__tests__/ReactDOMFiber-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1127,8 +1127,8 @@ describe('disableNewFiberFeatures', () => {
return props.children;
}

expect(() => ReactDOM.render(<Render>Hi</Render>, container)).toThrow(/You returned string/);
expect(() => ReactDOM.render(<Render>{999}</Render>, container)).toThrow(/You returned number/);
expect(() => ReactDOM.render(<Render>Hi</Render>, container)).toThrow(/You returned a string/);
expect(() => ReactDOM.render(<Render>{999}</Render>, container)).toThrow(/You returned a number/);
expect(() => ReactDOM.render(<Render>[<div />]</Render>, container)).toThrow(/You returned an array/);
});

Expand Down
21 changes: 17 additions & 4 deletions src/renderers/shared/fiber/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,20 @@ const {
Deletion,
} = ReactTypeOfSideEffect;

function coerceRef(current: Fiber | null, element: ReactElement) {
function getElementTypeForWarning(element) {
if (element === null) {
return 'null';
}
if (element === undefined) {
return 'undefined'
}
if (Array.isArray(element)) {
return 'an array';
}
return `a ${typeof element}`; // number, string, Symbol, boolean
}

function coerceRef(current: ?Fiber, element: ReactElement) {
let mixedRef = element.ref;
if (mixedRef !== null && typeof mixedRef !== 'function') {
if (element._owner) {
Expand Down Expand Up @@ -1240,10 +1253,10 @@ function ChildReconciler(shouldClone, shouldTrackSideEffects) {
const Component = returnFiber.type;
invariant(
newChild === null || newChild === false,
'%s(...): A valid React element (or null) must be returned. ' +
'You may have returned undefined, an array or some other ' +
'invalid object.',
'%s(...) must return a valid React element (or null). ' +
'You returned %s.',
Component.displayName || Component.name || 'Component',
getElementTypeForWarning(newChild)
);
break;
}
Expand Down
22 changes: 4 additions & 18 deletions src/renderers/shared/stack/reconciler/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,13 @@ function getElementTypeForWarning(element) {
if (element === null) {
return 'null';
}
if (element === undefined) {
return 'undefined'
}
if (Array.isArray(element)) {
return 'an array';
}
return typeof element;
}

function warnIfInvalidElement(Component, element) {
if (__DEV__) {
warning(
element === null || element === false || React.isValidElement(element),
'%s(...) must return a valid React element (or null). ' +
'You returned %s.',
Component.displayName || Component.name || 'Component',
getElementTypeForWarning(element)
);
warning(
!Component.childContextTypes,
'%s(...): childContextTypes cannot be defined on a functional component.',
Component.displayName || Component.name || 'Component'
);
}
return `a ${typeof element}`; // number, string, Symbol, boolean
}

function shouldConstruct(Component) {
Expand Down

0 comments on commit f85b091

Please sign in to comment.