Skip to content

Commit cc2ac4b

Browse files
committed
support multiple componets, impliments #1
1 parent 2c25f15 commit cc2ac4b

File tree

11 files changed

+201
-30
lines changed

11 files changed

+201
-30
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@ No updates when you dont expect them. For all. For free!
1111
[![NPM](https://nodei.co/npm/beautiful-react-redux.png?downloads=true&stars=true)](https://nodei.co/npm/beautiful-react-redux/)
1212

1313
## Usage
14+
15+
#### Auto-magic
1416
```js
1517
// to get automatic deep-equal memoization for all mapStateToProps
16-
import 'beautiful-react-redux/memoize';
18+
import 'beautiful-react-redux/patch';
19+
```
20+
21+
#### Or just replace react-redux by beautiful-react-redux
22+
```js
23+
//import {connect, Provider} from 'react-redux';
24+
import {connect, Provider} from 'beautiful-react-redux';
1725
```
1826

1927
If you already handling selectors by your own, and dont need external tools -

lib/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.Provider = exports.connect = undefined;
7+
8+
var _react = require('react');
9+
10+
var _reactRedux = require('react-redux');
11+
12+
var _memoizeState = require('memoize-state');
13+
14+
var _memoizeState2 = _interopRequireDefault(_memoizeState);
15+
16+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17+
18+
var connect = function connect(mapStateToProps, mapDispatchToProps, mergeProps, options) {
19+
20+
if (options && 'pure' in options && !options.pure) {
21+
return (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps, mergeProps, options);
22+
}
23+
24+
var memoizedMapStateToProps = mapStateToProps && (0, _memoizeState2.default)(mapStateToProps, { strictArguments: true });
25+
26+
return function (WrappedComponent) {
27+
28+
var localMapStateToProps = memoizedMapStateToProps && mapStateToProps && (0, _memoizeState2.default)(memoizedMapStateToProps, { strictArguments: true });
29+
30+
// TODO: create `areStatesEqual` based on memoize-state usage.
31+
return (0, _reactRedux.connect)(localMapStateToProps, mapDispatchToProps, mergeProps, options)(WrappedComponent);
32+
};
33+
};
34+
35+
exports.connect = connect;
36+
exports.Provider = _reactRedux.Provider;

lib/memoize.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var _react = require('react');
4+
35
var _reactRedux = require('react-redux');
46

57
var redux = _interopRequireWildcard(_reactRedux);
@@ -12,12 +14,19 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
1214

1315
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
1416

15-
var c = redux.connect;
17+
var reactReduxConnect = redux.connect;
1618
redux.connect = function (mapStateToProps, mapDispatchToProps, mergeProps, options) {
1719

1820
if (options && 'pure' in options && options.pure) {
1921
return c.call(redux, mapStateToProps, mapDispatchToProps, mergeProps, options);
2022
}
2123

22-
return c.call(redux, mapStateToProps && (0, _memoizeState2.default)(mapStateToProps, { strictArguments: true }), mapDispatchToProps, mergeProps, options);
24+
var memoizedMapStateToProps = mapStateToProps && (0, _memoizeState2.default)(mapStateToProps, { strictArguments: true });
25+
26+
return function (WrappedComponent) {
27+
28+
var ReduxedComponent = reactReduxConnect.call(redux, memoizedMapStateToProps && mapStateToProps && (0, _memoizeState2.default)(memoizedMapStateToProps, { strictArguments: true }), mapDispatchToProps, mergeProps, options)(WrappedComponent);
29+
30+
return ReduxedComponent;
31+
};
2332
};

lib/patch.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
var _reactRedux = require('react-redux');
4+
5+
var redux = _interopRequireWildcard(_reactRedux);
6+
7+
var _index = require('./index');
8+
9+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
10+
11+
redux.connect = _index.connect;

memoize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
require('./lib/memoize.js');
1+
require('./lib/patch.js');

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@
2323
},
2424
"author": "Anton Korzunov (thekashey@gmail.com)",
2525
"license": "MIT",
26+
"peerDependencies": {
27+
"react": "^15.0.0 || ^16.0.0"
28+
},
2629
"devDependencies": {
2730
"babel-cli": "^6.26.0",
2831
"babel-preset-env": "^1.6.1",
2932
"chai": "^4.1.2",
30-
"mocha": "^4.0.1"
33+
"mocha": "^4.0.1",
34+
"react": "^16.2.0"
3135
},
3236
"repository": {
3337
"type": "git",

patch.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./lib/patch.js');

src/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {Component} from 'react';
2+
import {connect as reactReduxConnect, Provider} from 'react-redux';
3+
import memoize from 'memoize-state';
4+
5+
const connect = (mapStateToProps,
6+
mapDispatchToProps,
7+
mergeProps,
8+
options) => {
9+
10+
if (options && ('pure' in options) && !options.pure) {
11+
return reactReduxConnect(
12+
mapStateToProps,
13+
mapDispatchToProps,
14+
mergeProps,
15+
options
16+
)
17+
}
18+
19+
const memoizedMapStateToProps = mapStateToProps && memoize(mapStateToProps, {strictArguments: true});
20+
21+
return WrappedComponent => {
22+
23+
const localMapStateToProps = memoizedMapStateToProps && mapStateToProps && memoize(memoizedMapStateToProps, {strictArguments: true});
24+
25+
// TODO: create `areStatesEqual` based on memoize-state usage.
26+
return reactReduxConnect(
27+
localMapStateToProps,
28+
mapDispatchToProps,
29+
mergeProps,
30+
options
31+
)(WrappedComponent);
32+
}
33+
};
34+
35+
export {
36+
connect,
37+
Provider
38+
}

src/memoize.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/patch.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import * as redux from 'react-redux';
2+
import {connect} from './index';
3+
4+
redux.connect = connect;

0 commit comments

Comments
 (0)