Skip to content

Feature: add object shorthand for mapStateToProps in connect #1566

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

Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ website/build/
website/yarn.lock
website/node_modules
website/i18n/*

.DS_Store
20 changes: 19 additions & 1 deletion docs/api/connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The returns of `mapStateToProps` and `mapDispatchToProps` are referred to intern

`connect` accepts four different parameters, all optional. By convention, they are called:

1. `mapStateToProps?: Function`
1. `mapStateToProps?: Function | Object`
2. `mapDispatchToProps?: Function | Object`
3. `mergeProps?: Function`
4. `options?: Object`
Expand Down Expand Up @@ -73,6 +73,24 @@ For more details on recommended usage of `mapStateToProps`, please refer to [our

> You may define `mapStateToProps` and `mapDispatchToProps` as a factory function, i.e., you return a function instead of an object. In this case your returned function will be treated as the real `mapStateToProps` or `mapDispatchToProps`, and be called in subsequent calls. You may see notes on [Factory Functions](#factory-functions) or our guide on performance optimizations.

#### Object Shorthand Form

`mapStateToProps` may be an object where each field is an [selector](https://redux.js.org/recipes/computing-derived-data#selectorsindexjs).

```js
import { getTodos } from './selectors'

const mapStateToProps = {
todos: getTodos,
}

export default connect(
mapStateToProps,
)(TodoApp)
```

In this case, React-Redux binds the `state` and `ownProps` of your store to each of the selector using `createStructuredSelector` from [reselect](https://github.com/reduxjs/reselect).

### `mapDispatchToProps?: Object | (dispatch, ownProps?) => Object`

Conventionally called `mapDispatchToProps`, this second parameter to `connect()` may either be an object, a function, or not supplied.
Expand Down
2 changes: 2 additions & 0 deletions docs/introduction/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ yarn add react-redux

You'll also need to [install Redux](https://redux.js.org/introduction/installation) and [set up a Redux store](https://redux.js.org/recipes/configuring-your-store/) in your app.

Also need to [install Reselect](https://github.com/reduxjs/reselect#installation) to use object shorthand for `mapStateToProps` in `connect`.

## `Provider`

React Redux provides `<Provider />`, which makes the Redux store available to the rest of your app:
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
},
"peerDependencies": {
"react": "^16.8.3",
"redux": "^2.0.0 || ^3.0.0 || ^4.0.0-0"
"redux": "^2.0.0 || ^3.0.0 || ^4.0.0-0",
"reselect": "^4.0.0"
},
"peerDependenciesMeta": {
"react-dom": {
Expand Down
17 changes: 16 additions & 1 deletion src/connect/mapStateToProps.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createStructuredSelector } from 'reselect'

import { wrapMapToPropsConstant, wrapMapToPropsFunc } from './wrapMapToProps'

export function whenMapStateToPropsIsFunction(mapStateToProps) {
Expand All @@ -10,4 +12,17 @@ export function whenMapStateToPropsIsMissing(mapStateToProps) {
return !mapStateToProps ? wrapMapToPropsConstant(() => ({})) : undefined
}

export default [whenMapStateToPropsIsFunction, whenMapStateToPropsIsMissing]
export function whenMapStateToPropsIsObject(mapStateToProps) {
return mapStateToProps && typeof mapStateToProps === 'object'
? wrapMapToPropsFunc(
createStructuredSelector(mapStateToProps),
'mapStateToProps'
)
: undefined
}

export default [
whenMapStateToPropsIsFunction,
whenMapStateToPropsIsMissing,
whenMapStateToPropsIsObject
]