diff --git a/src/Concur/React.purs b/src/Concur/React.purs index 50f7d5c..3c9f519 100644 --- a/src/Concur/React.purs +++ b/src/Concur/React.purs @@ -6,6 +6,7 @@ import Concur.Core.Discharge (discharge, dischargePartialEffect) import Concur.Core.Types (Widget) import Data.Either (Either(..)) import Data.Tuple (Tuple(..)) +import Effect (Effect) import Effect.Console (log) import React as R @@ -19,22 +20,25 @@ type ComponentState mkComponentState :: HTML -> ComponentState mkComponentState v = { view: v } -componentClass :: forall a. Widget HTML a -> R.ReactClass {} -componentClass winit = R.component "Concur" component +componentClassWithMount :: forall a. Effect Unit -> Widget HTML a -> R.ReactClass {} +componentClassWithMount onMount winit = R.component "Concur" component where - component this = do - Tuple winit' v <- dischargePartialEffect winit - pure { state: mkComponentState v - , render: render <$> R.getState this - , componentDidMount: handler this (Right winit') - } - handler this (Right r) = do - v <- discharge (handler this) r - void $ R.writeState this (mkComponentState v) - handler _ (Left err) = do - log ("FAILED! " <> show err) - pure unit - render st = R.toElement st.view + component this = do + Tuple winit' v <- dischargePartialEffect winit + pure { state: mkComponentState v + , render: render <$> R.getState this + , componentDidMount: onMount *> handler this (Right winit') + } + handler this (Right r) = do + v <- discharge (handler this) r + void $ R.writeState this (mkComponentState v) + handler _ (Left err) = do + log ("FAILED! " <> show err) + pure unit + render st = R.toElement st.view + +componentClass :: forall a. Widget HTML a -> R.ReactClass {} +componentClass = componentClassWithMount mempty renderComponent :: forall a. Widget HTML a -> R.ReactElement renderComponent init = R.createLeafElement (componentClass init) {}