From c8b710c7a6d347624f2e74d6c99efb55b2179f9e Mon Sep 17 00:00:00 2001 From: Anupam Jain Date: Wed, 29 Jan 2020 11:15:11 +0530 Subject: [PATCH] HACK: Added `componentClassWithMount` which allows using the componentDidMount event. See https://github.com/purescript-concur/purescript-concur-react/issues/39 --- src/Concur/React.purs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) 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) {}