Skip to content

Commit b9e6186

Browse files
author
方永乐
committed
add fetchData, redux connect, react router 4 with router decorators
1 parent ea3cd90 commit b9e6186

14 files changed

+954
-0
lines changed

.babelrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"presets": [
3+
"env",
4+
"react",
5+
"stage-3"
6+
],
7+
"plugins": [
8+
"syntax-dynamic-import",
9+
"transform-runtime",
10+
"transform-decorators-legacy"
11+
]
12+
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@ typings/
5757
# dotenv environment variables file
5858
.env
5959

60+
*~
61+
.DS_Store
62+
.idea/

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
src/**/*
2+
src/

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
## react-web-helper
2+
3+
* helper for [create-react-web](https://github.com/skyFi/create-react-web)
4+
5+
### Usage
6+
7+
#### only redux connect
8+
9+
```JavaScript
10+
// before
11+
import React from 'react';
12+
import { connect } from 'react-redux';
13+
14+
class Example extends React.Component {
15+
//...
16+
}
17+
export default connect(/*mapStateToProps, etc.*/)(Example);
18+
19+
// after
20+
import React from 'react';
21+
import { reduxConnect } from 'react-web-helper';
22+
23+
@reduxConnect(/*mapStateToProps, etc.*/)
24+
class Example extends React.Component {
25+
//...
26+
}
27+
export default Example;
28+
```
29+
30+
#### react-router 4.x withRouter + redux
31+
32+
```JavaScript
33+
// before
34+
import React from 'react';
35+
import { connect } from 'react-redux';
36+
import { withRouter } from 'react-router-dom';
37+
38+
class Example extends React.Component {
39+
//...
40+
}
41+
export default withRouter(connect(/*mapStateToProps, etc.*/)(Example));
42+
43+
// after
44+
import React from 'react';
45+
import { reduxConnect, withRouter } from 'react-web-helper';
46+
47+
@withRouter()
48+
@reduxConnect(/*mapStateToProps, etc.*/)
49+
class Example extends React.Component {
50+
//...
51+
}
52+
export default Example;
53+
```
54+
55+
#### fetchData for SSR
56+
57+
```JavaScript
58+
// simple
59+
import React from 'react';
60+
import { fetchData } from 'react-web-helper';
61+
62+
@fetchData()
63+
class Example extends React.Component {
64+
// fetchData will exec in server side or componentDidMount func
65+
static async fetchData({ dispatch, match }) {
66+
await dispatch(someAction());
67+
// ...
68+
}
69+
// ...
70+
}
71+
export default Example;
72+
73+
// with react router 4.x and redux
74+
import React from 'react';
75+
import { reduxConnect, withRouter, fetchData } from 'react-web-helper';
76+
77+
@fetchData()
78+
@withRouter()
79+
@reduxConnect(/*mapStateToProps, etc.*/)
80+
class Example extends React.Component {
81+
//...
82+
}
83+
export default Example;
84+
```

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import reduxConnect from './src/reduxConnectDecorator';
2+
import withRouter from './src/withRouterDecorator';
3+
import fetchData from './src/fetchDataDecoretor';
4+
5+
export default {
6+
reduxConnect,
7+
withRouter,
8+
fetchData
9+
};

lib/fetchDataDecoretor.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
8+
9+
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
10+
11+
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
12+
13+
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
14+
15+
var _createClass2 = require('babel-runtime/helpers/createClass');
16+
17+
var _createClass3 = _interopRequireDefault(_createClass2);
18+
19+
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
20+
21+
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
22+
23+
var _inherits2 = require('babel-runtime/helpers/inherits');
24+
25+
var _inherits3 = _interopRequireDefault(_inherits2);
26+
27+
var _react = require('react');
28+
29+
var _react2 = _interopRequireDefault(_react);
30+
31+
var _hoistNonReactStatics = require('hoist-non-react-statics');
32+
33+
var _hoistNonReactStatics2 = _interopRequireDefault(_hoistNonReactStatics);
34+
35+
var _reactRedux = require('react-redux');
36+
37+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
38+
39+
var fetchData = function fetchData() {
40+
return function (WrappedComponent) {
41+
var Enhance = function (_React$Component) {
42+
(0, _inherits3.default)(Enhance, _React$Component);
43+
44+
function Enhance() {
45+
(0, _classCallCheck3.default)(this, Enhance);
46+
return (0, _possibleConstructorReturn3.default)(this, (Enhance.__proto__ || (0, _getPrototypeOf2.default)(Enhance)).apply(this, arguments));
47+
}
48+
49+
(0, _createClass3.default)(Enhance, [{
50+
key: 'componentDidMount',
51+
value: function componentDidMount() {
52+
if (WrappedComponent.fetchData instanceof Function) {
53+
WrappedComponent.fetchData({
54+
dispatch: this.props.dispatch,
55+
match: this.props.match
56+
});
57+
}
58+
}
59+
}, {
60+
key: 'render',
61+
value: function render() {
62+
return _react2.default.createElement(WrappedComponent, this.props);
63+
}
64+
}]);
65+
return Enhance;
66+
}(_react2.default.Component);
67+
68+
return (0, _reactRedux.connect)()((0, _hoistNonReactStatics2.default)(Enhance, WrappedComponent));
69+
};
70+
};
71+
72+
exports.default = fetchData;

lib/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _reduxConnectDecorator = require('./src/reduxConnectDecorator');
8+
9+
var _reduxConnectDecorator2 = _interopRequireDefault(_reduxConnectDecorator);
10+
11+
var _withRouterDecorator = require('./src/withRouterDecorator');
12+
13+
var _withRouterDecorator2 = _interopRequireDefault(_withRouterDecorator);
14+
15+
var _fetchDataDecoretor = require('./src/fetchDataDecoretor');
16+
17+
var _fetchDataDecoretor2 = _interopRequireDefault(_fetchDataDecoretor);
18+
19+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20+
21+
exports.default = {
22+
reduxConnect: _reduxConnectDecorator2.default,
23+
withRouter: _withRouterDecorator2.default,
24+
fetchData: _fetchDataDecoretor2.default
25+
};

lib/reduxConnectDecorator.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _reactRedux = require('react-redux');
8+
9+
exports.default = function () {
10+
return _reactRedux.connect.apply(undefined, arguments);
11+
};

lib/withRouterDecorator.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _reactRouterDom = require('react-router-dom');
8+
9+
exports.default = function () {
10+
return _reactRouterDom.withRouter;
11+
};

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "react-web-helper",
3+
"version": "0.0.1",
4+
"description": "create react web helper",
5+
"main": "index.js",
6+
"repository": "https://github.com/skyFi/react-web-helper.git",
7+
"author": "skylor",
8+
"license": "MIT",
9+
"scripts": {
10+
"babel": "babel index.js src --out-dir lib"
11+
},
12+
"dependencies": {
13+
"babel-plugin-syntax-dynamic-import": "^6.18.0",
14+
"babel-plugin-transform-decorators-legacy": "^1.3.4",
15+
"babel-plugin-transform-runtime": "^6.23.0",
16+
"babel-preset-env": "^1.6.1",
17+
"babel-preset-react": "^6.24.1",
18+
"babel-preset-stage-3": "^6.24.1"
19+
}
20+
}

0 commit comments

Comments
 (0)