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

Make the GlobalStyleComponent call the base constructor with props. #2321

Conversation

jaydenseric
Copy link
Contributor

This PR ensures the GlobalStyleComponent created by createGlobalStyle calls the base constructor with props.

This is a React best practice for class components:

Class components should always call the base constructor with props.
https://reactjs.org/docs/state-and-lifecycle.html#adding-local-state-to-a-class

Not doing so can cause issues with this.props being undefined when rendered by libraries that are not very fault tolerant.

React's renderToString accounts for it here: https://github.com/facebook/react/blob/v16.7.0/packages/react-dom/src/server/ReactPartialRenderer.js#L532

Apollo's getDataFromTree used to account for it here: https://github.com/apollographql/react-apollo/blob/v2.2.4/src/getDataFromTree.ts#L80

Fixes jaydenseric/graphql-react#17 .

@jaydenseric
Copy link
Contributor Author

Flow types gave me hell, both in setting up VSCode to make this PR, and in trying to learn how to add types for props. Will the way I have done it allow people to use the component with no props at all?

@quantizor
Copy link
Contributor

Flow types gave me hell, both in setting up VSCode to make this PR, and in trying to learn how to add types for props. Will the way I have done it allow people to use the component with no props at all?

We don't export our flow types, so don't worry about that. They'll actually be removed from the lib in general probably for v5.

Copy link
Contributor

@quantizor quantizor left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@quantizor quantizor merged commit cd134dc into styled-components:master Jan 15, 2019
@jaydenseric jaydenseric deleted the globalstylecomponent-super-props branch January 15, 2019 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants