-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Components*ReactHooks recipes (#249)
* Add ComponentsReactHooks * Add ComponentsInputReactHooks * Add ComponentsMultiTypeReactHooks * Update README * Add render count to ComponentsReactHooks * Add render count to ComponentsInputReactHooks * Add render count to ComponentsMultiTypeReactHooks * Add render count to ComponentsHalogenHooks * Add render count to ComponentsInputHalogenHooks * Add render count to ComponentsMultiTypeHalogenHooks * s/re-rendered/rendered
- Loading branch information
1 parent
47b1a09
commit c1b7bfb
Showing
25 changed files
with
559 additions
and
3 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
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,15 @@ | ||
/bower_components/ | ||
/node_modules/ | ||
/.pulp-cache/ | ||
/output/ | ||
/generated-docs/ | ||
/.psc-package/ | ||
/.psc* | ||
/.purs* | ||
/.psa* | ||
/.spago | ||
/.cache/ | ||
/dist/ | ||
/web-dist/ | ||
/prod-dist/ | ||
/prod/ |
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,15 @@ | ||
# ComponentsInputReactHooks | ||
|
||
Each time the parent's state updates, it will pass a new prop value into the child, and the child will update accordingly. | ||
|
||
## Expected Behavior: | ||
|
||
### Browser | ||
|
||
The parent stores an `Int` as state. Clicking the buttons will increment/decrement that value. The children will display the result of a mathematical computation using that value. | ||
The parent will also display how many times it has been rendered. | ||
|
||
## Dependencies Used: | ||
|
||
[react](https://www.npmjs.com/package/react) | ||
[react-dom](https://www.npmjs.com/package/react-dom) |
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,11 @@ | ||
{ name = "ComponentsInputReactHooks" | ||
, dependencies = | ||
[ "console" | ||
, "effect" | ||
, "psci-support" | ||
, "react-basic-hooks" | ||
, "react-basic-dom" | ||
] | ||
, packages = ../../packages.dhall | ||
, sources = [ "recipes/ComponentsInputReactHooks/src/**/*.purs" ] | ||
} |
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,84 @@ | ||
module ComponentsInputReactHooks.Main where | ||
|
||
import Prelude | ||
import Data.Maybe (Maybe(..)) | ||
import Effect (Effect) | ||
import Effect.Exception (throw) | ||
import Effect.Unsafe (unsafePerformEffect) | ||
import React.Basic.DOM (render) | ||
import React.Basic.DOM as R | ||
import React.Basic.Events (handler_) | ||
import React.Basic.Hooks | ||
( Component | ||
, Render | ||
, UseEffect | ||
, UseRef | ||
, component | ||
, readRef | ||
, useEffectAlways | ||
, useRef | ||
, useState | ||
, writeRef | ||
, (/\) | ||
) | ||
import React.Basic.Hooks as React | ||
import Web.HTML (window) | ||
import Web.HTML.HTMLDocument (body) | ||
import Web.HTML.HTMLElement (toElement) | ||
import Web.HTML.Window (document) | ||
|
||
main :: Effect Unit | ||
main = do | ||
body <- body =<< document =<< window | ||
case body of | ||
Nothing -> throw "Could not find body." | ||
Just b -> do | ||
container <- mkContainer | ||
render (container unit) (toElement b) | ||
|
||
mkContainer :: Component Unit | ||
mkContainer = do | ||
display <- mkDisplay | ||
component "Container" \_ -> React.do | ||
parentRenders <- useRenderCount | ||
state /\ setState <- useState 0 | ||
pure | ||
$ R.div_ | ||
[ R.ul_ | ||
[ display state | ||
, display (state * 2) | ||
, display (state * 3) | ||
, display (state * 10) | ||
, display (state * state) | ||
] | ||
, R.button | ||
{ onClick: handler_ (setState (_ + 1)) | ||
, children: [ R.text "+1" ] | ||
} | ||
, R.button | ||
{ onClick: handler_ (setState (_ - 1)) | ||
, children: [ R.text "-1" ] | ||
} | ||
, R.p_ | ||
[ R.text ("Parent has been rendered " <> show parentRenders <> " time(s)") ] | ||
] | ||
|
||
mkDisplay :: Component Int | ||
mkDisplay = | ||
component "Display" \n -> | ||
pure | ||
$ R.div_ | ||
[ R.text "My input value is: " | ||
, R.strong_ [ R.text (show n) ] | ||
] | ||
|
||
useRenderCount :: forall a. Render a (UseEffect Unit (UseRef Int a)) Int | ||
useRenderCount = React.do | ||
rendersRef <- useRef 1 | ||
useEffectAlways do | ||
renders <- readRef rendersRef | ||
writeRef rendersRef (renders + 1) | ||
pure mempty | ||
let | ||
renders = unsafePerformEffect (readRef rendersRef) | ||
pure renders |
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,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>ComponentsInputReactHooks</title> | ||
</head> | ||
|
||
<body> | ||
<div id="root"></div> | ||
<script src="./index.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"use strict"; | ||
require("../../../output/ComponentsInputReactHooks.Main/index.js").main(); |
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,15 @@ | ||
/bower_components/ | ||
/node_modules/ | ||
/.pulp-cache/ | ||
/output/ | ||
/generated-docs/ | ||
/.psc-package/ | ||
/.psc* | ||
/.purs* | ||
/.psa* | ||
/.spago | ||
/.cache/ | ||
/dist/ | ||
/web-dist/ | ||
/prod-dist/ | ||
/prod/ |
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,15 @@ | ||
# ComponentsMultiTypeReactHooks | ||
|
||
Demonstrates a parent component with several children components, each with different prop types. | ||
|
||
## Expected Behavior: | ||
|
||
### Browser | ||
|
||
A toggle button, a count button, and an input field are the children of the parent. The user can modify the state of any of those inputs and then click the "Check now" button. This will update the most recent observed state. | ||
The parent will also display how many times it has been rendered. | ||
|
||
## Dependencies Used: | ||
|
||
[react](https://www.npmjs.com/package/react) | ||
[react-dom](https://www.npmjs.com/package/react-dom) |
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,11 @@ | ||
{ name = "ComponentsMultiTypeReactHooks" | ||
, dependencies = | ||
[ "console" | ||
, "effect" | ||
, "psci-support" | ||
, "react-basic-hooks" | ||
, "react-basic-dom" | ||
] | ||
, packages = ../../packages.dhall | ||
, sources = [ "recipes/ComponentsMultiTypeReactHooks/src/**/*.purs" ] | ||
} |
Oops, something went wrong.