Skip to content

Data: Allow undefined return from withSelect mapSelectToProps - #5421

Merged
aduth merged 2 commits into
masterfrom
update/with-select-return-undef
Mar 7, 2018
Merged

aduth merged 2 commits into
masterfrom
update/with-select-return-undef

Conversation

@aduth

@aduth aduth commented Mar 5, 2018

Copy link
Copy Markdown
Member

This pull request seeks to allow a falsey return value from a withSelect mapping function mapSelectToProps . This is a divergence from React Redux which enforces that this value be an object, leading to some awkward workarounds to ensure an object is returned. With these changes, a falsey value is assumed as equivalent to an empty object of props.

Further, the changes here resolve an issue where props may linger after a subsequent mapping call returns a different set of object keys. Previously we used a component's setState to update mapped props, but this behaves as a patch application. Previously React supported a this.replaceState method (deprecated in React 0.13) for this purpose, but it's easy enough to replicate with choosing an arbitrary key in state to apply the mapped value to.

Testing instructions:

Ensure unit tests pass:

npm run test-unit

@aduth aduth added the Framework Issues related to broader framework topics, especially as it relates to javascript label Mar 5, 2018
@aduth

aduth commented Mar 5, 2018

Copy link
Copy Markdown
Member Author

Noting that this behavior was anticipated as part of the documentation updates in #5379:

Specific implementation differences from Redux and React Redux:

[...]

  • In React Redux, a mapStateToProps function must return an object.
    • In @wordpress/data, a withSelect mapping function can return undefined if it has no props to inject.

@aduth
aduth merged commit a468cf5 into master Mar 7, 2018
@aduth
aduth deleted the update/with-select-return-undef branch March 7, 2018 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Framework Issues related to broader framework topics, especially as it relates to javascript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant