-
Notifications
You must be signed in to change notification settings - Fork 33
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
Showing
7 changed files
with
384 additions
and
402 deletions.
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
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 |
---|---|---|
@@ -1,56 +1,48 @@ | ||
module Context where | ||
|
||
import Prelude | ||
|
||
import Data.Maybe (Maybe(..)) | ||
import Effect (Effect) | ||
import React.Basic.DOM as R | ||
import React.Basic.Events (handler_) | ||
import React.Basic.Hooks (Context, CreateComponent, JSX, type (/\), component, contextProvider, createContext, element, fragment, useContext, useState, (/\)) | ||
import React.Basic.Hooks (type (/\), CreateComponent, JSX, ReactContext, component, createContext, element, provider, useContext, useState, (/\)) | ||
import React.Basic.Hooks as React | ||
|
||
mkContext :: CreateComponent {} | ||
mkContext = do | ||
|
||
counterContext <- createContext | ||
counterContext <- createContext (0 /\ pure unit) | ||
store <- mkStore counterContext | ||
counter <- mkCounter counterContext | ||
|
||
component "Context" \props -> React.do | ||
|
||
pure $ element store | ||
{ children: | ||
[ element counter {} | ||
, element counter {} | ||
, element counter {} | ||
] | ||
} | ||
|
||
mkStore | ||
:: Context (Int /\ (Effect Unit)) | ||
-> CreateComponent { children :: Array JSX } | ||
pure | ||
$ element store | ||
{ children: | ||
[ element counter {} | ||
, element counter {} | ||
, element counter {} | ||
] | ||
} | ||
|
||
mkStore :: | ||
ReactContext (Int /\ (Effect Unit)) -> | ||
CreateComponent { children :: Array JSX } | ||
mkStore context = do | ||
component "Store" \{ children } -> React.do | ||
counter /\ setCounter <- useState 0 | ||
let increment = setCounter (_ + 1) | ||
pure $ | ||
contextProvider context | ||
(counter /\ increment) | ||
(fragment children) | ||
|
||
mkCounter | ||
:: Context (Int /\ (Effect Unit)) | ||
-> CreateComponent {} | ||
let | ||
increment = setCounter (_ + 1) | ||
pure | ||
$ provider context | ||
(counter /\ increment) | ||
children | ||
|
||
mkCounter :: | ||
ReactContext (Int /\ (Effect Unit)) -> | ||
CreateComponent {} | ||
mkCounter counterContext = do | ||
component "Counter" \props -> React.do | ||
mCounter <- useContext counterContext | ||
|
||
case mCounter of | ||
Nothing -> do | ||
pure $ R.text "no counter value found" | ||
Just (counter /\ increment) -> do | ||
pure $ | ||
R.button | ||
{ onClick: handler_ increment | ||
, children: [ R.text $ "Increment: " <> show counter ] | ||
} | ||
counter /\ increment <- useContext counterContext | ||
pure | ||
$ R.button | ||
{ onClick: handler_ increment | ||
, children: [ R.text $ "Increment: " <> show counter ] | ||
} |
Oops, something went wrong.