From 86ed7a791581b76af8976bc82d0f7c7f6f3205c6 Mon Sep 17 00:00:00 2001 From: Michael Trotter Date: Mon, 1 Oct 2018 12:17:18 -0600 Subject: [PATCH] Fix setStateThen's state arg (#56) --- bower.json | 3 ++- examples/component/src/ToggleButton.purs | 6 ++++-- src/React/Basic.js | 14 +++++++++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/bower.json b/bower.json index dc054bb..182b0f3 100644 --- a/bower.json +++ b/bower.json @@ -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" } } diff --git a/examples/component/src/ToggleButton.purs b/examples/component/src/ToggleButton.purs index 852b44a..9b546bc 100644 --- a/examples/component/src/ToggleButton.purs +++ b/examples/component/src/ToggleButton.purs @@ -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 @@ -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 diff --git a/src/React/Basic.js b/src/React/Basic.js index 28ddad3..a2181c1 100644 --- a/src/React/Basic.js +++ b/src/React/Basic.js @@ -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; @@ -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 }); }; @@ -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 }); }; @@ -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 }); };