Skip to content

Force update #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ function createClass(spec) {
}
exports.createClass = createClass;

function forceUpdateCbImpl(this_, cb) {
this_.forceUpdate(function() {
return cb();
});
return {};
};
exports.forceUpdateCbImpl = forceUpdateCbImpl;

function handle(f) {
return function(e){
return f(e)();
Expand Down
21 changes: 21 additions & 0 deletions src/React.purs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ module React
, writeStateWithCallback
, transformState

, forceUpdate
, forceUpdateCb

, handle
, preventDefault
, stopPropagation
Expand All @@ -65,7 +68,9 @@ module React
) where

import Prelude

import Control.Monad.Eff (kind Effect, Eff)
import Control.Monad.Eff.Uncurried (EffFn2, runEffFn2)
import Unsafe.Coerce (unsafeCoerce)

-- | Name of a tag.
Expand Down Expand Up @@ -335,6 +340,22 @@ createClassStateless' k =
createClassStateless \props ->
k props (childrenToArray (unsafeCoerce props).children)

-- | Force render of a react component.
forceUpdate :: forall eff props state.
ReactThis props state -> Eff eff Unit
forceUpdate this = forceUpdateCb this (pure unit)

foreign import forceUpdateCbImpl :: forall eff e props state.
EffFn2 eff
(ReactThis props state)
(Eff e Unit)
Unit

-- | Force render and then run an Eff computation.
forceUpdateCb :: forall eff props state.
ReactThis props state -> Eff eff Unit -> Eff eff Unit
forceUpdateCb this m = runEffFn2 forceUpdateCbImpl this m

-- | Create an event handler.
foreign import handle :: forall eff ev props state result.
(ev -> EventHandlerContext eff props state result) -> EventHandler ev
Expand Down