-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove update/action code and re-expose React's setState functions (#73)
We've been finding the update/action pattern to be too verbose for basic use cases. ~There was also a bug with type checking actions correctly (#71).~ This update keeps the current types and lifecycle code while returning state updates to the more traditional `setState` and `setStateThen`.
- Loading branch information
1 parent
e567488
commit 542decc
Showing
23 changed files
with
389 additions
and
438 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
output | ||
html/index.js | ||
package-lock.json | ||
node_modules |
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,8 @@ | ||
all: node_modules | ||
purs compile src/*.purs '../../src/**/*.purs' '../../bower_components/purescript-*/src/**/*.purs' | ||
purs bundle -m Main --main Main output/*/*.js > output/bundle.js | ||
node_modules/.bin/browserify output/bundle.js -o html/index.js | ||
|
||
node_modules: | ||
npm install | ||
|
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 @@ | ||
# Counter Example | ||
|
||
## Building | ||
|
||
``` | ||
npm install | ||
make all | ||
``` | ||
|
||
This will compile the PureScript source files, bundle them, and use Browserify to combine PureScript and NPM sources into a single bundle. | ||
|
||
Then open `html/index.html` in your browser. |
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,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>react-basic example</title> | ||
</head> | ||
<body> | ||
<div id="container"></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,9 @@ | ||
{ | ||
"dependencies": { | ||
"react": "16.6.0", | ||
"react-dom": "16.6.0" | ||
}, | ||
"devDependencies": { | ||
"browserify": "16.2.3" | ||
} | ||
} |
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,37 @@ | ||
module Actions where | ||
|
||
import Prelude | ||
|
||
import Effect.Console (log) | ||
import React.Basic (Component, JSX, StateUpdate(..), createComponent, make, runUpdate) | ||
import React.Basic.DOM as R | ||
import React.Basic.DOM.Events (capture_) | ||
|
||
component :: Component Props | ||
component = createComponent "Counter" | ||
|
||
type Props = | ||
{ label :: String | ||
} | ||
|
||
data Action | ||
= Increment | ||
|
||
actions :: Props -> JSX | ||
actions = make component { initialState, render } | ||
where | ||
initialState = { counter: 0 } | ||
|
||
update self = case _ of | ||
Increment -> | ||
UpdateAndSideEffects | ||
(self.state { counter = self.state.counter + 1 }) | ||
\{ state } -> log $ "Count: " <> show state.counter | ||
|
||
send = runUpdate update | ||
|
||
render self = | ||
R.button | ||
{ onClick: capture_ $ send self Increment | ||
, children: [ R.text (self.props.label <> ": " <> show self.state.counter) ] | ||
} |
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,22 @@ | ||
module Main where | ||
|
||
import Prelude | ||
|
||
import Actions (actions) | ||
import Data.Maybe (Maybe(..)) | ||
import Effect (Effect) | ||
import Effect.Exception (throw) | ||
import React.Basic.DOM (render) | ||
import Web.DOM.NonElementParentNode (getElementById) | ||
import Web.HTML (window) | ||
import Web.HTML.HTMLDocument (toNonElementParentNode) | ||
import Web.HTML.Window (document) | ||
|
||
main :: Effect Unit | ||
main = do | ||
container <- getElementById "container" =<< (map toNonElementParentNode $ document =<< window) | ||
case container of | ||
Nothing -> throw "Container element not found." | ||
Just c -> | ||
let app = actions { label: "Increment" } | ||
in render app c |
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 |
---|---|---|
|
@@ -4,6 +4,6 @@ | |
"react-dom": "^16.4.2" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^16.2.2" | ||
"browserify": "16.2.3" | ||
} | ||
} |
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
Oops, something went wrong.