-
Notifications
You must be signed in to change notification settings - Fork 50.4k
[Fiber] Throw on undefined component output #8648
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
da3dc03
Add a test for stateless components returning undefined
gaearon 243d245
Remove a duplicate test
gaearon 2646272
Remove unnecessary warning for invalid node
gaearon 8c4b2a6
Make test that forbids arrays Stack-only
gaearon 297d89b
Record Fiber tests
gaearon 5643a2c
Throw on undefined component output
gaearon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,12 +19,19 @@ import type { FiberRoot } from 'ReactFiberRoot'; | |
| import type { HostConfig } from 'ReactFiberReconciler'; | ||
| import type { PriorityLevel } from 'ReactPriorityLevel'; | ||
|
|
||
| var { | ||
| isValidElement, | ||
| } = require('React'); | ||
| var { | ||
| mountChildFibersInPlace, | ||
| reconcileChildFibers, | ||
| reconcileChildFibersInPlace, | ||
| cloneChildFibers, | ||
| } = require('ReactChildFiber'); | ||
| var { | ||
| isCoroutine, | ||
| isYield, | ||
| } = require('ReactCoroutine'); | ||
| var { | ||
| beginUpdateQueue, | ||
| } = require('ReactFiberUpdateQueue'); | ||
|
|
@@ -49,6 +56,9 @@ var { | |
| YieldComponent, | ||
| Fragment, | ||
| } = ReactTypeOfWork; | ||
| var { | ||
| isPortal, | ||
| } = require('ReactPortal'); | ||
| var { | ||
| NoWork, | ||
| OffscreenPriority, | ||
|
|
@@ -62,6 +72,8 @@ var { | |
| var ReactCurrentOwner = require('ReactCurrentOwner'); | ||
| var ReactFiberClassComponent = require('ReactFiberClassComponent'); | ||
|
|
||
| var invariant = require('invariant'); | ||
|
|
||
| if (__DEV__) { | ||
| var ReactDebugCurrentFiber = require('ReactDebugCurrentFiber'); | ||
| } | ||
|
|
@@ -88,6 +100,46 @@ module.exports = function<T, P, I, TI, C, CX>( | |
| updateClassInstance, | ||
| } = ReactFiberClassComponent(scheduleUpdate, getPriorityContext); | ||
|
|
||
| function isValidNode(node) { | ||
| switch (typeof node) { | ||
| case 'string': | ||
| case 'number': | ||
| return true; | ||
| case 'boolean': | ||
| return node === false; | ||
| case 'object': | ||
| return ( | ||
| node === null || | ||
| Array.isArray(node) || | ||
| isValidElement(node) || | ||
| isPortal(node) || | ||
| isCoroutine(node) || | ||
| isYield(node) | ||
| ); | ||
| case 'undefined': | ||
| return false; | ||
| default: | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function throwInvalidNodeError() { | ||
| // TODO: report the node type and/or object keys. | ||
| var info = ''; | ||
| if (__DEV__) { | ||
| var name = ReactDebugCurrentFiber.getCurrentFiberOwnerName(); | ||
| if (name) { | ||
| info += ' Check the render method of `' + name + '`.'; | ||
| } | ||
| } | ||
| invariant( | ||
| false, | ||
| 'A valid React element, null, or an array must be returned. ' + | ||
| 'You may have returned undefined or some other invalid object.%s', | ||
| info | ||
| ); | ||
| } | ||
|
|
||
| function markChildAsProgressed(current, workInProgress, priorityLevel) { | ||
| // We now have clones. Let's store them as the currently progressed work. | ||
| workInProgress.progressedChild = workInProgress.child; | ||
|
|
@@ -211,6 +263,9 @@ module.exports = function<T, P, I, TI, C, CX>( | |
| } else { | ||
| nextChildren = fn(nextProps, context); | ||
| } | ||
| if (!isValidNode(nextChildren)) { | ||
| throwInvalidNodeError(); | ||
| } | ||
| reconcileChildren(current, workInProgress, nextChildren); | ||
| return workInProgress.child; | ||
| } | ||
|
|
@@ -263,7 +318,19 @@ module.exports = function<T, P, I, TI, C, CX>( | |
| // Rerender | ||
| const instance = workInProgress.stateNode; | ||
| ReactCurrentOwner.current = workInProgress; | ||
| const nextChildren = instance.render(); | ||
| let nextChildren = instance.render(); | ||
| if (__DEV__) { | ||
| // We allow auto-mocks to proceed as if they're returning null. | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is copy paste from CompositeComponent. |
||
| if (nextChildren === undefined && | ||
| instance.render._isMockFunction) { | ||
| // This is probably bad practice. Consider warning here and | ||
| // deprecating this convenience. | ||
| nextChildren = null; | ||
| } | ||
| } | ||
| if (!isValidNode(nextChildren)) { | ||
| throwInvalidNodeError(); | ||
| } | ||
| reconcileChildren(current, workInProgress, nextChildren); | ||
|
|
||
| // The context might have changed so we need to recalculate it. | ||
|
|
@@ -433,11 +500,13 @@ module.exports = function<T, P, I, TI, C, CX>( | |
| adoptClassInstance(workInProgress, value); | ||
| mountClassInstance(workInProgress, priorityLevel); | ||
| return finishClassComponent(current, workInProgress, true, hasContext); | ||
| } else { | ||
| } else if (isValidNode(value)) { | ||
| // Proceed under the assumption that this is a functional component | ||
| workInProgress.tag = FunctionalComponent; | ||
| reconcileChildren(current, workInProgress, value); | ||
| return workInProgress.child; | ||
| } else { | ||
| throwInvalidNodeError(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,26 +44,9 @@ function StatelessComponent(Component) { | |
| StatelessComponent.prototype.render = function() { | ||
| var Component = ReactInstanceMap.get(this)._currentElement.type; | ||
| var element = Component(this.props, this.context, this.updater); | ||
| warnIfInvalidElement(Component, element); | ||
| return element; | ||
| }; | ||
|
|
||
| function warnIfInvalidElement(Component, element) { | ||
| if (__DEV__) { | ||
| warning( | ||
| element === null || element === false || React.isValidElement(element), | ||
| '%s(...): A valid React element (or null) must be returned. You may have ' + | ||
| 'returned undefined, an array or some other invalid object.', | ||
| Component.displayName || Component.name || 'Component' | ||
| ); | ||
| warning( | ||
| !Component.childContextTypes, | ||
| '%s(...): childContextTypes cannot be defined on a functional component.', | ||
| Component.displayName || Component.name || 'Component' | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function shouldConstruct(Component) { | ||
| return !!(Component.prototype && Component.prototype.isReactComponent); | ||
| } | ||
|
|
@@ -210,7 +193,13 @@ var ReactCompositeComponent = { | |
| // Support functional components | ||
| if (!doConstruct && (inst == null || inst.render == null)) { | ||
| renderedElement = inst; | ||
| warnIfInvalidElement(Component, renderedElement); | ||
| if (__DEV__) { | ||
| warning( | ||
| !Component.childContextTypes, | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is copy paste from |
||
| '%s(...): childContextTypes cannot be defined on a functional component.', | ||
| Component.displayName || Component.name || 'Component' | ||
| ); | ||
| } | ||
| invariant( | ||
| inst === null || | ||
| inst === false || | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like that this is a duplicate runtime type check with the branch check that is done in ReactChildFiber.
IMO we should move this validation into the ReactChildFiber since you can just throw if you fall out into the last branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a good guideline is that we shouldn't have any production invariants unless the branch is already covered regardless.