Skip to content
This repository has been archived by the owner on Dec 23, 2019. It is now read-only.

Commit

Permalink
Fix for setState callback
Browse files Browse the repository at this point in the history
Patch this React bug:
- facebook/react#1740
-facebook/react#11507
  • Loading branch information
cvburgess committed Dec 5, 2017
1 parent 6f07b52 commit 1deeacf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
18 changes: 11 additions & 7 deletions lib/withData.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ function withData(actions) {
actionNames.forEach(function (actionName) {
data[actionName] = {
loading: true,
applyParams: function applyParams(params) {
return _this.applyParams(actionName, params);
},
refetch: function refetch() {
return _this.getData(actionName, { noCache: true });
}
Expand Down Expand Up @@ -115,7 +112,7 @@ function withData(actions) {

this.applyParams = function (actionName, params) {
_this3.setState(function (prevState) {
(0, _immutabilityHelper2.default)(prevState, {
return (0, _immutabilityHelper2.default)(prevState, {
data: _defineProperty({}, actionName, { loading: { $set: true } }),
appliedParams: _defineProperty({}, actionName, { $merge: params })
});
Expand All @@ -126,8 +123,15 @@ function withData(actions) {

this.onNext = function (actionName, data) {
_this3.setState(function (prevState) {
(0, _immutabilityHelper2.default)(prevState, {
data: _defineProperty({}, actionName, { $merge: _extends({ loading: false }, data) })
return (0, _immutabilityHelper2.default)(prevState, {
data: _defineProperty({}, actionName, {
$merge: _extends({
loading: false,
applyParams: function applyParams(params) {
return _this3.applyParams(actionName, params);
}
}, data)
})
});
});
};
Expand All @@ -136,7 +140,7 @@ function withData(actions) {
var isAxiosError = Object.keys(error).includes("request"); // crude, but works
if (isAxiosError) {
_this3.setState(function (prevState) {
(0, _immutabilityHelper2.default)(prevState, {
return (0, _immutabilityHelper2.default)(prevState, {
data: _defineProperty({}, actionName, { $merge: { error: error, loading: false } })
});
});
Expand Down
26 changes: 15 additions & 11 deletions src/withData.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ function withData(actions) {
actionNames.forEach(actionName => {
data[actionName] = {
loading: true,
applyParams: params => this.applyParams(actionName, params),
refetch: () => this.getData(actionName, { noCache: true })
};
appliedParams[actionName] = {};
Expand All @@ -29,38 +28,43 @@ function withData(actions) {

applyParams = (actionName, params) => {
this.setState(
prevState => {
prevState =>
update(prevState, {
data: { [actionName]: { loading: { $set: true } } },
appliedParams: { [actionName]: { $merge: params } }
});
},
}),
() => this.getData(actionName)
);
};

// Set data[actionName] from { loading: false } + the result of the request
onNext = (actionName, data) => {
this.setState(prevState => {
this.setState(prevState =>
update(prevState, {
data: {
[actionName]: { $merge: { loading: false, ...data } }
[actionName]: {
$merge: {
loading: false,
applyParams: params => this.applyParams(actionName, params),
...data
}
}
}
});
});
})
);
};

// Set data[actionName] from { loading: false } + the { error } generated by Axios
onError = (actionName, error) => {
const isAxiosError = Object.keys(error).includes("request"); // crude, but works
if (isAxiosError) {
this.setState(prevState => {
this.setState(prevState =>
update(prevState, {
data: {
[actionName]: { $merge: { error, loading: false } }
}
});
});
})
);
} else {
throw error;
}
Expand Down

0 comments on commit 1deeacf

Please sign in to comment.