Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ module.exports = {
selector: 'CallExpression[callee.object.name="Math"][callee.property.name="random"]',
message: 'Do not use Math.random() to generate unique IDs; use withInstanceId instead. (If you’re not generating unique IDs: ignore this message.)',
},
{
selector: 'CallExpression[callee.name="withDispatch"] > :function > BlockStatement > :not(VariableDeclaration,ReturnStatement)',
message: 'withDispatch must return an object with consistent keys. Avoid performing logic in `mapDispatchToProps`.',
},
],
'react/forbid-elements': [ 'error', {
forbid: [
Expand Down
2 changes: 2 additions & 0 deletions packages/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ const SaleButton = withDispatch( ( dispatch, ownProps ) => {
// <SaleButton>Start Sale!</SaleButton>
```

*Note:* It is important that the `mapDispatchToProps` function always returns an object with the same keys. For example, it should not contain conditions under which a different value would be returned.

## Generic Stores

The `@wordpress/data` module offers a more advanced and generic interface for the purposes of integrating other data systems and situations where more direct control over a data system is needed. In this case, a data store will need to be implemented outside of `@wordpress/data` and then plugged in via three functions:
Expand Down
94 changes: 44 additions & 50 deletions packages/data/src/components/with-dispatch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { mapValues } from 'lodash';
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { pure, compose, createHigherOrderComponent } from '@wordpress/compose';
import { createHigherOrderComponent } from '@wordpress/compose';

/**
* Internal dependencies
Expand All @@ -27,63 +27,57 @@ import { RegistryConsumer } from '../registry-provider';
* @return {Component} Enhanced component with merged dispatcher props.
*/
const withDispatch = ( mapDispatchToProps ) => createHigherOrderComponent(
compose( [
pure,
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to remove pure?

Copy link
Member Author

Choose a reason for hiding this comment

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

Any reason to remove pure?

The thought was since we no longer respond to props changes, nor perform any expensive operations in the render, they could just go straight through.

The main downside I might see is if someone had come to rely on withDispatch for its pure behavior, which is and always would be a fragile dependency.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm fine with this PR. Ultimately, I think we should try to find a way to measure these PRs somehow, I know it's very difficult to do and I'd appreciate thoughts on that as I want to measure my own performance PRs as well.

( WrappedComponent ) => {
class ComponentWithDispatch extends Component {
constructor( props ) {
super( ...arguments );
( WrappedComponent ) => {
class ComponentWithDispatch extends Component {
constructor( props ) {
super( ...arguments );

this.proxyProps = {};
this.setProxyProps( props );
}
this.proxyProps = {};

componentDidUpdate() {
this.setProxyProps( this.props );
}
this.setProxyProps( props );
}

proxyDispatch( propName, ...args ) {
// Original dispatcher is a pre-bound (dispatching) action creator.
mapDispatchToProps( this.props.registry.dispatch, this.props.ownProps )[ propName ]( ...args );
}
proxyDispatch( propName, ...args ) {
// Original dispatcher is a pre-bound (dispatching) action creator.
mapDispatchToProps( this.props.registry.dispatch, this.props.ownProps )[ propName ]( ...args );
}

setProxyProps( props ) {
// Assign as instance property so that in subsequent render
// reconciliation, the prop values are referentially equal.
// Importantly, note that while `mapDispatchToProps` is
// called, it is done only to determine the keys for which
// proxy functions should be created. The actual registry
// dispatch does not occur until the function is called.
const propsToDispatchers = mapDispatchToProps( this.props.registry.dispatch, props.ownProps );
this.proxyProps = mapValues( propsToDispatchers, ( dispatcher, propName ) => {
// Prebind with prop name so we have reference to the original
// dispatcher to invoke. Track between re-renders to avoid
// creating new function references every render.
if ( this.proxyProps.hasOwnProperty( propName ) ) {
return this.proxyProps[ propName ];
}
setProxyProps( props ) {
// Assign as instance property so that in subsequent render
// reconciliation, the prop values are referentially equal.
// Importantly, note that while `mapDispatchToProps` is
// called, it is done only to determine the keys for which
// proxy functions should be created. The actual registry
// dispatch does not occur until the function is called.
const propsToDispatchers = mapDispatchToProps( this.props.registry.dispatch, props.ownProps );
this.proxyProps = mapValues( propsToDispatchers, ( dispatcher, propName ) => {
// Prebind with prop name so we have reference to the original
// dispatcher to invoke. Track between re-renders to avoid
// creating new function references every render.
if ( this.proxyProps.hasOwnProperty( propName ) ) {
return this.proxyProps[ propName ];
}

return this.proxyDispatch.bind( this, propName );
} );
}
return this.proxyDispatch.bind( this, propName );
} );
}

render() {
return <WrappedComponent { ...this.props.ownProps } { ...this.proxyProps } />;
}
render() {
return <WrappedComponent { ...this.props.ownProps } { ...this.proxyProps } />;
}
}

return ( ownProps ) => (
<RegistryConsumer>
{ ( registry ) => (
<ComponentWithDispatch
ownProps={ ownProps }
registry={ registry }
/>
) }
</RegistryConsumer>
);
},
] ),
return ( ownProps ) => (
<RegistryConsumer>
{ ( registry ) => (
<ComponentWithDispatch
ownProps={ ownProps }
registry={ registry }
/>
) }
</RegistryConsumer>
);
},
'withDispatch'
);

Expand Down
5 changes: 2 additions & 3 deletions packages/data/src/components/with-select/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@ describe( 'withSelect', () => {
testInstance.findByType( 'button' ).props.onClick();

expect( testInstance.findByType( 'button' ).props.children ).toBe( 1 );
// 3 times =
// 2 times =
// 1. Initial mount
// 2. When click handler is called
// 3. After select updates its merge props
expect( mapDispatchToProps ).toHaveBeenCalledTimes( 3 );
expect( mapDispatchToProps ).toHaveBeenCalledTimes( 2 );
expect( mapSelectToProps ).toHaveBeenCalledTimes( 2 );
expect( OriginalComponent ).toHaveBeenCalledTimes( 2 );
} );
Expand Down