Skip to content

Commit 5816287

Browse files
author
方永乐
committed
add notPreventFirstFetchOnBroswer to fetchData
1 parent 50c8d3d commit 5816287

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,15 @@ export default Example;
7474
import React from 'react';
7575
import { reduxConnect, withRouter, fetchData } from 'react-web-helper';
7676

77-
@fetchData()
77+
@fetchData(/*notPreventFirstFetchOnBroswer*/)
7878
@withRouter()
7979
@reduxConnect(/*mapStateToProps, etc.*/)
8080
class Example extends React.Component {
8181
//...
8282
}
8383
export default Example;
8484
```
85+
86+
* @params {boolean} notPreventFirstFetchOnBroswer
87+
88+
not frevent first load fetchData on broswer, default value is `false`.

lib/fetchDataDecoretor.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ var _reactRedux = require('react-redux');
3636

3737
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
3838

39+
var isFirstLoadOnServer = true;
3940
var fetchData = function fetchData() {
41+
var notPreventFirstFetchOnBroswer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
42+
4043
return function (WrappedComponent) {
4144
var Enhance = function (_React$Component) {
4245
(0, _inherits3.default)(Enhance, _React$Component);
@@ -49,11 +52,14 @@ var fetchData = function fetchData() {
4952
(0, _createClass3.default)(Enhance, [{
5053
key: 'componentDidMount',
5154
value: function componentDidMount() {
52-
if (WrappedComponent.fetchData instanceof Function) {
53-
WrappedComponent.fetchData({
54-
dispatch: this.props.dispatch,
55-
match: this.props.match
56-
});
55+
if (isFirstLoadOnServer || notPreventFirstFetchOnBroswer) {
56+
isFirstLoadOnServer = false;
57+
if (WrappedComponent.fetchData instanceof Function) {
58+
WrappedComponent.fetchData({
59+
dispatch: this.props.dispatch,
60+
match: this.props.match
61+
});
62+
}
5763
}
5864
}
5965
}, {

src/fetchDataDecoretor.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ import React from 'react';
22
import hoistNonReactStatic from 'hoist-non-react-statics';
33
import { connect } from 'react-redux';
44

5-
const fetchData = () => {
5+
let isFirstLoadOnServer = true;
6+
const fetchData = (notPreventFirstFetchOnBroswer = false) => {
67
return (WrappedComponent) => {
78
class Enhance extends React.Component {
89
componentDidMount() {
9-
if (WrappedComponent.fetchData instanceof Function) {
10-
WrappedComponent.fetchData({
11-
dispatch: this.props.dispatch,
12-
match: this.props.match
13-
});
10+
if (isFirstLoadOnServer || notPreventFirstFetchOnBroswer) {
11+
isFirstLoadOnServer = false;
12+
if (WrappedComponent.fetchData instanceof Function) {
13+
WrappedComponent.fetchData({
14+
dispatch: this.props.dispatch,
15+
match: this.props.match
16+
});
17+
}
1418
}
1519
}
1620
render() {

0 commit comments

Comments
 (0)