-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Try adding a pure Higher Order Component #6313
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import { | |
isString, | ||
upperFirst, | ||
} from 'lodash'; | ||
import isShallowEqual from 'shallowequal'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -209,3 +210,34 @@ export function RawHTML( { children, ...props } ) { | |
...props, | ||
} ); | ||
} | ||
|
||
/** | ||
* Given a component returns the enhanced component augmented with a component | ||
* only rerendering when its props/state change | ||
* | ||
* @param {Function} mapComponentToEnhancedComponent Function mapping component | ||
* to enhanced component. | ||
* @param {string} modifierName Seed name from which to | ||
* generated display name. | ||
* | ||
* @return {WPComponent} Component class with generated display name assigned. | ||
*/ | ||
export const purify = createHigherOrderComponent( ( Wrapped ) => { | ||
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. Why did you choose to export this in FWIW, I had a branch with similar change, and encountered that 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. I asked myself several times about
I'm sure this is all about dependencies. There needs to be a solution :) 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. I actually didn't think that much about it. I put it under element to match 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. Well explained, I'm sold :) 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. Hmm, ignoring the fact that React does it, I don't see why, for example, we'd want to export a 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.
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. The alternative perspective is that components is a baseline, and where we're encountering circular dependencies is a sign where we have our dependencies inversed. Considering
(This is problematic in #5316 in introducing the reverse dependency from Popover to Viewport) Where maybe it ought to be:
This highlights a third type of component: Data-bound components. We could treat them all separately, e.g. 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. So did we just not decide anything here? 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. I assumed it's going to be renamed to 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. It was ... I guess you are referring to the location of this HOC, we need to come up with something sooner than later 👍 |
||
if ( Wrapped.prototype instanceof Component ) { | ||
return class extends Wrapped { | ||
shouldComponentUpdate( nextProps, nextState ) { | ||
return ! isShallowEqual( nextProps, this.props ) || ! isShallowEqual( nextState, this.state ); | ||
} | ||
}; | ||
} | ||
|
||
return class extends Component { | ||
shouldComponentUpdate( nextProps ) { | ||
return ! isShallowEqual( nextProps, this.props ); | ||
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. Interesting conclusion that we don't need to compare state here, and I suppose it makes sense since it's wrapping the original (non-class) component. 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. Agreed on this one, there is no state in functional components 👍 |
||
} | ||
|
||
render() { | ||
return <Wrapped { ...this.props } />; | ||
} | ||
}; | ||
}, 'purify' ); |
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.
Can this be expressed as: