Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Resolve defaultProps for lazy etc #1218

Merged
merged 1 commit into from
Nov 7, 2018
Merged

Resolve defaultProps for lazy etc #1218

merged 1 commit into from
Nov 7, 2018

Conversation

gaearon
Copy link
Contributor

@gaearon gaearon commented Nov 7, 2018

Related to facebook/react#13902.

If fiber.elementType exists and differs from fiber.type, we need to respect the defaultProps defined on the fiber.type. Such default props wouldn't be kept in fiber.memoizedProps. This is why in the React code we also call resolveDefaultProps() in those cases.

I added a regression case to demonstrate it. It should display <FooWithDefaultProps greeting="Hello" name="World" />. In master, it is missing name="World" (that’s the issue I am fixing).

typeof fiber.elementType !== undefined &&
fiber.type !== fiber.elementType
) {
props = resolveDefaultProps(fiber.type, props);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could've put this into a more specific place, like Function + Class + ForwardRef + Memo or whichever ones support it. But I found this easier to read. The only way you could have a false positive here is if somebody assigned defaultProps to e.g. a context consumer. I think that's unlikely enough that it would be fine for DevTools to mistakingly resolve them for a type that doesn't support them — to keep the code readable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems safe. I think any time fiber.type !== fiber.elementType, it's a lazy component.

<LazyWithDefaultProps greeting="Hello" />
</React.Suspense>
</Feature>
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! ❤️

typeof fiber.elementType !== undefined &&
fiber.type !== fiber.elementType
) {
props = resolveDefaultProps(fiber.type, props);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems safe. I think any time fiber.type !== fiber.elementType, it's a lazy component.

@gaearon gaearon merged commit 536a54a into master Nov 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants