Skip to content

Commit

Permalink
Fix setStateThen's state arg (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
spicydonuts authored Oct 1, 2018
1 parent f44fb99 commit 86ed7a7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"purescript-exceptions": "^4.0.0"
},
"devDependencies": {
"purescript-web-html": "^1.0.0"
"purescript-web-html": "^1.0.0",
"purescript-console": "^4.1.0"
}
}
6 changes: 4 additions & 2 deletions examples/component/src/ToggleButton.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module ToggleButton where

import Prelude

import Effect.Console (log)
import React.Basic as React
import React.Basic.DOM as R
import React.Basic.Events as Events
Expand All @@ -20,10 +21,11 @@ component = React.component { displayName: "ToggleButton", initialState, receive
receiveProps _ =
pure unit

render { props, state, setState } =
render { props, state, setStateThen } =
R.button
{ onClick: Events.handler_ do
setState \s -> s { on = not s.on }
setStateThen (\s -> s { on = not s.on }) \nextState -> do
log $ "nextState: " <> show nextState
, children:
[ R.text props.label
, R.text if state.on
Expand Down
14 changes: 11 additions & 3 deletions src/React/Basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
var React = require("react");
var Fragment = React.Fragment || "div";

function setStateThen(instance) {
return function(update, then) {
return instance.setState(update, function() {
then(this.state);
});
};
}

exports.component_ = function(spec) {
var Component = function constructor() {
this.state = spec.initialState;
Expand All @@ -20,7 +28,7 @@ exports.component_ = function(spec) {
props: this.props,
state: this.state,
setState: this._setState,
setStateThen: this._setState,
setStateThen: setStateThen(this),
instance_: this
});
};
Expand All @@ -31,7 +39,7 @@ exports.component_ = function(spec) {
props: this.props,
state: this.state,
setState: this._setState,
setStateThen: this._setState,
setStateThen: setStateThen(this),
instance_: this
});
};
Expand All @@ -41,7 +49,7 @@ exports.component_ = function(spec) {
props: this.props,
state: this.state,
setState: this._setState,
setStateThen: this._setState,
setStateThen: setStateThen(this),
instance_: this
});
};
Expand Down

0 comments on commit 86ed7a7

Please sign in to comment.