Skip to content

Commit 7cb1b53

Browse files
Cootethul
Coot
authored andcommitted
Force update (#103)
* add forceUpdate function * forceUpdateCb * forceUpdate: implement using forceUpdateCb * remove forceUpdateImpl from ffi
1 parent f753b9f commit 7cb1b53

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/React.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ function createClass(spec) {
122122
}
123123
exports.createClass = createClass;
124124

125+
function forceUpdateCbImpl(this_, cb) {
126+
this_.forceUpdate(function() {
127+
return cb();
128+
});
129+
return {};
130+
};
131+
exports.forceUpdateCbImpl = forceUpdateCbImpl;
132+
125133
function handle(f) {
126134
return function(e){
127135
return f(e)();

src/React.purs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ module React
5050
, writeStateWithCallback
5151
, transformState
5252

53+
, forceUpdate
54+
, forceUpdateCb
55+
5356
, handle
5457
, preventDefault
5558
, stopPropagation
@@ -65,7 +68,9 @@ module React
6568
) where
6669

6770
import Prelude
71+
6872
import Control.Monad.Eff (kind Effect, Eff)
73+
import Control.Monad.Eff.Uncurried (EffFn2, runEffFn2)
6974
import Unsafe.Coerce (unsafeCoerce)
7075

7176
-- | Name of a tag.
@@ -335,6 +340,22 @@ createClassStateless' k =
335340
createClassStateless \props ->
336341
k props (childrenToArray (unsafeCoerce props).children)
337342

343+
-- | Force render of a react component.
344+
forceUpdate :: forall eff props state.
345+
ReactThis props state -> Eff eff Unit
346+
forceUpdate this = forceUpdateCb this (pure unit)
347+
348+
foreign import forceUpdateCbImpl :: forall eff e props state.
349+
EffFn2 eff
350+
(ReactThis props state)
351+
(Eff e Unit)
352+
Unit
353+
354+
-- | Force render and then run an Eff computation.
355+
forceUpdateCb :: forall eff props state.
356+
ReactThis props state -> Eff eff Unit -> Eff eff Unit
357+
forceUpdateCb this m = runEffFn2 forceUpdateCbImpl this m
358+
338359
-- | Create an event handler.
339360
foreign import handle :: forall eff ev props state result.
340361
(ev -> EventHandlerContext eff props state result) -> EventHandler ev

0 commit comments

Comments
 (0)