Skip to content

Commit 75bc88b

Browse files
committed
Merge pull request #62 from spencerjanssen/transform_state
Fix transformState to use current state
2 parents f9b9849 + 809ad59 commit 75bc88b

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/React.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ function readState(this_) {
6060
}
6161
exports.readState = readState;
6262

63+
function transformState(this_){
64+
return function(update){
65+
return function(){
66+
this_.setState(function(old, props){
67+
return {state: update(old.state)};
68+
});
69+
};
70+
};
71+
}
72+
exports.transformState = transformState;
73+
6374
function createClass(spec) {
6475
var result = {
6576
displayName: spec.displayName,

src/React.purs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,7 @@ foreign import writeState :: forall props state access eff. ReactThis props stat
285285
foreign import readState :: forall props state access eff. ReactThis props state -> Eff (state :: ReactState (read :: Read | access) | eff) state
286286

287287
-- | Transform the component state by applying a function.
288-
transformState :: forall props state eff. ReactThis props state -> (state -> state) -> Eff (state :: ReactState ReadWrite | eff) state
289-
transformState ctx f = do
290-
state <- readState ctx
291-
writeState ctx $ f state
288+
foreign import transformState :: forall props state eff. ReactThis props state -> (state -> state) -> Eff (state :: ReactState ReadWrite | eff) Unit
292289

293290
-- | Create a React class from a specification.
294291
foreign import createClass :: forall props state eff. ReactSpec props state eff -> ReactClass props

0 commit comments

Comments
 (0)