Skip to content

Commit c8b710c

Browse files
committed
HACK: Added componentClassWithMount which allows using the componentDidMount event.
See #39
1 parent b7aaaf4 commit c8b710c

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/Concur/React.purs

+19-15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Concur.Core.Discharge (discharge, dischargePartialEffect)
66
import Concur.Core.Types (Widget)
77
import Data.Either (Either(..))
88
import Data.Tuple (Tuple(..))
9+
import Effect (Effect)
910
import Effect.Console (log)
1011
import React as R
1112

@@ -19,22 +20,25 @@ type ComponentState
1920
mkComponentState :: HTML -> ComponentState
2021
mkComponentState v = { view: v }
2122

22-
componentClass :: forall a. Widget HTML a -> R.ReactClass {}
23-
componentClass winit = R.component "Concur" component
23+
componentClassWithMount :: forall a. Effect Unit -> Widget HTML a -> R.ReactClass {}
24+
componentClassWithMount onMount winit = R.component "Concur" component
2425
where
25-
component this = do
26-
Tuple winit' v <- dischargePartialEffect winit
27-
pure { state: mkComponentState v
28-
, render: render <$> R.getState this
29-
, componentDidMount: handler this (Right winit')
30-
}
31-
handler this (Right r) = do
32-
v <- discharge (handler this) r
33-
void $ R.writeState this (mkComponentState v)
34-
handler _ (Left err) = do
35-
log ("FAILED! " <> show err)
36-
pure unit
37-
render st = R.toElement st.view
26+
component this = do
27+
Tuple winit' v <- dischargePartialEffect winit
28+
pure { state: mkComponentState v
29+
, render: render <$> R.getState this
30+
, componentDidMount: onMount *> handler this (Right winit')
31+
}
32+
handler this (Right r) = do
33+
v <- discharge (handler this) r
34+
void $ R.writeState this (mkComponentState v)
35+
handler _ (Left err) = do
36+
log ("FAILED! " <> show err)
37+
pure unit
38+
render st = R.toElement st.view
39+
40+
componentClass :: forall a. Widget HTML a -> R.ReactClass {}
41+
componentClass = componentClassWithMount mempty
3842

3943
renderComponent :: forall a. Widget HTML a -> R.ReactElement
4044
renderComponent init = R.createLeafElement (componentClass init) {}

0 commit comments

Comments
 (0)