Skip to content

Commit

Permalink
Try adding a purify Higher Order Component
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Apr 20, 2018
1 parent cb1b92e commit 1f428d3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
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 {
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 ) => {
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 );
}

render() {
return <Wrapped { ...this.props } />;
}
};
}, 'purify' );

0 comments on commit 1f428d3

Please sign in to comment.