-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
abf7d72
commit f889de1
Showing
10 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Hooks.Counter where | ||
|
||
import Prelude | ||
|
||
import Basic.Store as BS | ||
import Data.Maybe (fromMaybe) | ||
import Halogen as H | ||
import Halogen.HTML as HH | ||
import Halogen.HTML.Events as HE | ||
import Halogen.Hooks as Hooks | ||
import Halogen.Store.Hooks.UseSelector (useSelector) | ||
import Halogen.Store.Monad (class MonadStore, updateStore) | ||
import Halogen.Store.Select (selectEq) | ||
|
||
component :: forall q i o m | ||
. MonadStore BS.Action BS.Store m | ||
=> H.Component q i o m | ||
component = Hooks.component \_ _ -> Hooks.do | ||
count <- useSelector $ selectEq _.count | ||
Hooks.pure do | ||
let cnt = fromMaybe 0 count | ||
HH.div_ | ||
[ HH.button | ||
[ HE.onClick \_ -> updateStore BS.Increment ] | ||
[ HH.text "Increment"] | ||
, HH.text $ " Count: " <> show cnt <> " " | ||
, HH.button | ||
[ HE.onClick \_ -> updateStore BS.Decrement ] | ||
[ HH.text "Decrement" ] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Hooks.Main where | ||
|
||
import Prelude | ||
|
||
import Basic.Counter as Counter | ||
import Basic.Store as BS | ||
import Effect (Effect) | ||
import Effect.Aff (launchAff_) | ||
import Halogen.Aff as HA | ||
import Halogen.Store.Monad (runStoreT) | ||
import Halogen.VDom.Driver (runUI) | ||
|
||
main :: Effect Unit | ||
main = launchAff_ do | ||
body <- HA.awaitBody | ||
root <- runStoreT BS.initialStore BS.reduce Counter.component | ||
runUI root unit body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Hooks.Store where | ||
|
||
import Prelude | ||
|
||
type Store = { count :: Int } | ||
|
||
initialStore :: Store | ||
initialStore = { count: 0 } | ||
|
||
data Action | ||
= Increment | ||
| Decrement | ||
|
||
reduce :: Store -> Action -> Store | ||
reduce store = case _ of | ||
Increment -> store { count = store.count + 1 } | ||
Decrement -> store { count = store.count - 1 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Hooks Example | ||
|
||
The basic-hooks example is yet another alternative to the basic example. It demonstrates how to access a small store from a single component (a counter) using hooks functionality (`useSeletor`) instead of stateful component. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Halogen Store - Basic with Hooks</title> | ||
<style> | ||
body { | ||
font-family: sans-serif; | ||
max-width: 800px; | ||
margin: auto; | ||
padding: 50px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<script src="app.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Halogen.Store.Hooks.UseSelector where | ||
|
||
import Prelude | ||
|
||
import Data.Maybe (Maybe(..)) | ||
import Data.Tuple.Nested ((/\)) | ||
import Halogen.Hooks (class HookNewtype, type (<>), Hook, UseEffect, UseState) | ||
import Halogen.Hooks as Hooks | ||
import Halogen.Store.Monad (class MonadStore, emitSelected) | ||
import Halogen.Store.Select (Selector(..)) | ||
|
||
foreign import data UseSelector :: Type -> Type -> Type -> Hooks.HookType | ||
|
||
type UseSelector' :: Type -> Type -> Type -> Hooks.HookType | ||
type UseSelector' act store ctx = UseState (Maybe ctx) <> UseEffect <> Hooks.Pure | ||
|
||
instance newtypeUseSelector | ||
:: HookNewtype (UseSelector act store ctx) (UseSelector' act store ctx) | ||
|
||
useSelector :: forall m act store ctx | ||
. MonadStore act store m | ||
=> Selector store ctx | ||
-> Hook m (UseSelector act store ctx) (Maybe ctx) | ||
useSelector (Selector selector) = Hooks.wrap hook | ||
where | ||
hook :: Hook m (UseSelector' act store ctx) (Maybe ctx) | ||
hook = Hooks.do | ||
ctx /\ ctxId <- Hooks.useState Nothing | ||
|
||
Hooks.useLifecycleEffect do | ||
emitter <- emitSelected (Selector selector) | ||
subscriptionId <- Hooks.subscribe $ map (Hooks.put ctxId <<< Just) emitter | ||
pure $ Just $ Hooks.unsubscribe subscriptionId | ||
|
||
Hooks.pure ctx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters