Skip to content

Commit 86ed7a7

Browse files
authored
Fix setStateThen's state arg (#56)
1 parent f44fb99 commit 86ed7a7

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"purescript-exceptions": "^4.0.0"
2424
},
2525
"devDependencies": {
26-
"purescript-web-html": "^1.0.0"
26+
"purescript-web-html": "^1.0.0",
27+
"purescript-console": "^4.1.0"
2728
}
2829
}

examples/component/src/ToggleButton.purs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module ToggleButton where
22

33
import Prelude
44

5+
import Effect.Console (log)
56
import React.Basic as React
67
import React.Basic.DOM as R
78
import React.Basic.Events as Events
@@ -20,10 +21,11 @@ component = React.component { displayName: "ToggleButton", initialState, receive
2021
receiveProps _ =
2122
pure unit
2223

23-
render { props, state, setState } =
24+
render { props, state, setStateThen } =
2425
R.button
2526
{ onClick: Events.handler_ do
26-
setState \s -> s { on = not s.on }
27+
setStateThen (\s -> s { on = not s.on }) \nextState -> do
28+
log $ "nextState: " <> show nextState
2729
, children:
2830
[ R.text props.label
2931
, R.text if state.on

src/React/Basic.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
var React = require("react");
44
var Fragment = React.Fragment || "div";
55

6+
function setStateThen(instance) {
7+
return function(update, then) {
8+
return instance.setState(update, function() {
9+
then(this.state);
10+
});
11+
};
12+
}
13+
614
exports.component_ = function(spec) {
715
var Component = function constructor() {
816
this.state = spec.initialState;
@@ -20,7 +28,7 @@ exports.component_ = function(spec) {
2028
props: this.props,
2129
state: this.state,
2230
setState: this._setState,
23-
setStateThen: this._setState,
31+
setStateThen: setStateThen(this),
2432
instance_: this
2533
});
2634
};
@@ -31,7 +39,7 @@ exports.component_ = function(spec) {
3139
props: this.props,
3240
state: this.state,
3341
setState: this._setState,
34-
setStateThen: this._setState,
42+
setStateThen: setStateThen(this),
3543
instance_: this
3644
});
3745
};
@@ -41,7 +49,7 @@ exports.component_ = function(spec) {
4149
props: this.props,
4250
state: this.state,
4351
setState: this._setState,
44-
setStateThen: this._setState,
52+
setStateThen: setStateThen(this),
4553
instance_: this
4654
});
4755
};

0 commit comments

Comments
 (0)