-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Conditionally use PureComponent
instead of Component
#1335
Conversation
…omponent` instead of `React.Component`
src/components/CalendarDay.jsx
Outdated
class CalendarDay extends React.Component { | ||
const BaseClass = baseClass(); | ||
|
||
class CalendarDay extends BaseClass { |
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.
this is going to break react linting; we'll need a jsdoc comment to indicate that it extends React.Component
src/components/CalendarDay.jsx
Outdated
@@ -48,17 +48,15 @@ const defaultProps = { | |||
phrases: CalendarDayPhrases, | |||
}; | |||
|
|||
class CalendarDay extends React.Component { | |||
const BaseClass = baseClass(); |
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.
extends baseClass()
works too
src/utils/baseClass.js
Outdated
|
||
export const pureComponentAvailable = typeof React.PureComponent !== 'undefined'; | ||
|
||
export default function baseClass(pureComponent = pureComponentAvailable) { |
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.
why is baseClass
a function, but pureComponentAvailable
is not?
I'd expect either both to be a function (to support polyfilling of PureComponent, i assume), or neither.
- Both `pureComponentAvailable` and `baseClass` are now functions - Using `extends baseClass()` instead of defining `BaseClass = baseClass()` - Fixing the lint on `DayPickerRangeController` for `pureComponentAvailable` being defined but not used
@ljharb what does the jsdoc comment need to be? Your linting system is different than I am used to working with. |
(what are you used to working with that’s not eslint?) i believe it’s |
@ljharb not in that way. I guess I never knew about being able to do that is a better way of describing it. |
@AntiFish03 here's an example: jsx-eslint/eslint-plugin-react#513 (comment) |
@AntiFish03 what's the reasoning that the baseClass stuff needs to be in a function, instead of caching it once at module load time? |
It doesn't have to be. Just trying to keep it similar to what is used in react-with-styles as was requested. |
…oad, instead of using a function.
travis-ci decided to have some timeout issues there for no apparent reason... @ljharb care to restart that and everything should be good to go |
After a few discussions, i wonder if it'd be worth it to try something like this? /** @extends React.Component */
class Foo extends React.PureComponent || React.Component {
[React.PureComponent || 'shouldComponentUpdate'](nextProps, nextState) {
return shallowCompare(this, nextProps, nextState);
}
} Otherwise, your change LGTM. |
@ljharb, Change made as requested anything else? |
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.
With appropriate documentation - including telling people that they can overwrite build/utils/baseClass.js
with module.exports = React.PureComponent
if they know they're using a newer React - this LGTM.
@ljharb, Readme updated. |
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.
This looks great! Thank you so much for making this change.
.storybook/config.js
Outdated
@@ -1,4 +1,9 @@ | |||
import React from 'react'; | |||
if (process.env.NODE_ENV !== 'production') { | |||
const {whyDidYouUpdate} = require('why-did-you-update'); |
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.
nit: spaces around the whyDidYouUpdate
const { whyDidYouUpdate } = require('why-did-you-update');
src/utils/baseClass.js
Outdated
import shallowCompare from 'react-addons-shallow-compare'; | ||
|
||
class BaseClass extends (React.PureComponent || React.Component) { | ||
[React.PureComponent || 'shouldComponentUpdate'](nextProps, nextState) { |
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'm not sure I fully understand what this will do in the case that React.PureComponent
is defined
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.
oh right, this should be React.PureComponent &&
- so that it's undefined
in that case, instead of the method name
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.
shouldn't it actually be !React.PureComponent &&
so that it injects the shouldComponentUpdate
when PureComponent isn't available.
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.
aha, yes, that's better. coding is hard.
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.
We're all tired. :P
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.
Gonna put an X on this temporarily so that ||
doesn't accidentally get merged in
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.
So fun fact, I believe that this breaks react-dates in IE10. sigh It seems to be related to how this is transformed by babel (current working theory is something about the double-extends). |
…Upgrade"" This reverts commit 50c382f.
@majapw all of a sudden I am getting
Is this something you are seeing also? |
@AntiFish03 locally or in storybook? on the latest version? |
@majapw, Locally from master. |
Working to resolve #1297. Conditionally using
React.PureComponent
instead ofReact.Component