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

Try adding a pure Higher Order Component #6313

Merged
merged 3 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import memoize from 'memize';
/**
* WordPress dependencies
*/
import { Component, createHigherOrderComponent } from '@wordpress/element';
import { Component, createHigherOrderComponent, purify } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -242,7 +242,7 @@ export function dispatch( reducerKey ) {
* @return {Component} Enhanced component with merged state data props.
*/
export const withSelect = ( mapStateToProps ) => createHigherOrderComponent( ( WrappedComponent ) => {
return class ComponentWithSelect extends Component {
Copy link
Member

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:

createHigherOrderComponent( compose( [
	purify,
	( WrappedComponent ) => class extends Component {

	}
] ), 'withSelect' );

return purify( class ComponentWithSelect extends Component {
constructor() {
super( ...arguments );

Expand All @@ -251,10 +251,6 @@ export const withSelect = ( mapStateToProps ) => createHigherOrderComponent( ( W
this.state = {};
}

shouldComponentUpdate( nextProps, nextState ) {
return ! isShallowEqual( nextProps, this.props ) || ! isShallowEqual( nextState, this.state );
}

componentWillMount() {
this.subscribe();

Expand Down Expand Up @@ -300,7 +296,7 @@ export const withSelect = ( mapStateToProps ) => createHigherOrderComponent( ( W
render() {
return <WrappedComponent { ...this.props } { ...this.state.mergeProps } />;
}
};
} );
}, 'withSelect' );

/**
Expand All @@ -316,7 +312,7 @@ export const withSelect = ( mapStateToProps ) => createHigherOrderComponent( ( W
* @return {Component} Enhanced component with merged dispatcher props.
*/
export const withDispatch = ( mapDispatchToProps ) => createHigherOrderComponent( ( WrappedComponent ) => {
return class ComponentWithDispatch extends Component {
return purify( class ComponentWithDispatch extends Component {
constructor() {
super( ...arguments );

Expand Down Expand Up @@ -355,7 +351,7 @@ export const withDispatch = ( mapDispatchToProps ) => createHigherOrderComponent
render() {
return <WrappedComponent { ...this.props } { ...this.proxyProps } />;
}
};
} );
}, 'withDispatch' );

/**
Expand Down
2 changes: 1 addition & 1 deletion editor/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,5 +705,5 @@ export default compose(
};
} ),
withFilters( 'editor.BlockListBlock' ),
withHoverAreas
withHoverAreas,
)( BlockListBlock );
32 changes: 32 additions & 0 deletions element/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
isString,
upperFirst,
} from 'lodash';
import isShallowEqual from 'shallowequal';

/**
* Internal dependencies
Expand Down Expand Up @@ -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 ) => {
Copy link
Member

Choose a reason for hiding this comment

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

Why did you choose to export this in element, and not components ?

FWIW, I had a branch with similar change, and encountered that data would then depend on components, which surfaced yet another issue of: Should withSelect and withDispatch be in data or not?

Copy link
Member

Choose a reason for hiding this comment

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

I asked myself several times about withFilters in components which depends on hooks. A very similar case.

Why did you choose to export this in element, and not components ?

I'm sure this is all about dependencies. There needs to be a solution :)

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 actually didn't think that much about it. I put it under element to match React :) which also provides a pure component definition.

Copy link
Member

Choose a reason for hiding this comment

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

Well explained, I'm sold :)

Copy link
Member

Choose a reason for hiding this comment

The 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 pure higher-order component in element, but ifCondition higher-order component in components. What judgement do we use to categorize them?

Copy link
Member

Choose a reason for hiding this comment

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

pure, ifCondition, withState, withInstanceId, but also createHigherOrderComponent and compose - we can put all of them together in their own module 💯

Copy link
Member

Choose a reason for hiding this comment

The 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 viewport and the issues highlighted in #5316, the current dependency hierarchy is:

Viewport (ifViewportMatches) > Components (ifCondition)

(This is problematic in #5316 in introducing the reverse dependency from Popover to Viewport)

Where maybe it ought to be:

Components (ifViewportMatches + ifCondition) > Viewport (withSelect( 'viewport' ) )

This highlights a third type of component: Data-bound components.

We could treat them all separately, e.g. higher-order-components, app-components, ui-components, though it's not clear to me whether this will ultimately solve all of our issues, and of course has added overhead in making determinations of which components / utilities belong where.

Copy link
Member

Choose a reason for hiding this comment

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

So did we just not decide anything here?

Copy link
Member

Choose a reason for hiding this comment

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

I assumed it's going to be renamed to pure.

Copy link
Member

Choose a reason for hiding this comment

The 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 );
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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' );