Skip to content

Commit 5c44118

Browse files
committed
Counter example (no webpack)
1 parent 833a86d commit 5c44118

File tree

7 files changed

+84
-0
lines changed

7 files changed

+84
-0
lines changed

examples/counter/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output
2+
html/index.js
3+
node_modules

examples/counter/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
all:
2+
purs compile src/*.purs '../../src/**/*.purs' '../../bower_components/purescript-*/src/**/*.purs'
3+
purs bundle --module Counter output/*/*.js > output/bundle.js
4+
echo 'module.exports = PS.Counter;' >> output/bundle.js
5+
node_modules/browserify/bin/cmd.js output/bundle.js index.js -o html/index.js

examples/counter/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Counter Example
2+
3+
## Building
4+
5+
```
6+
npm install
7+
make all
8+
```
9+
10+
This will compile the PureScript source files, bundle them, and use Browserify to combine PureScript and NPM sources into a single bundle.
11+
12+
Then open `html/index.html` in your browser.

examples/counter/html/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>react-basic example</title>
5+
</head>
6+
<body>
7+
<div id="container"></div>
8+
<script src="index.js"></script>
9+
</body>
10+
</html>

examples/counter/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
3+
var React = require("react");
4+
var ReactDOM = require("react-dom");
5+
var Counter = require("./output/bundle.js");
6+
7+
ReactDOM.render(
8+
React.createElement(Counter.component, { label: 'Increment' }),
9+
document.getElementById("container")
10+
);

examples/counter/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "counter",
3+
"version": "1.0.0",
4+
"description": "",
5+
"keywords": [],
6+
"author": "",
7+
"dependencies": {
8+
"create-react-class": "^15.6.2",
9+
"react": "^15.6.2",
10+
"react-dom": "^15.6.2"
11+
},
12+
"devDependencies": {
13+
"browserify": "^16.1.0"
14+
}
15+
}

examples/counter/src/Counter.purs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Counter where
2+
3+
import Prelude
4+
5+
import Control.Monad.Eff.Uncurried (mkEffFn1)
6+
import React.Basic as R
7+
8+
-- The props for the component
9+
type ExampleProps =
10+
{ label :: String
11+
}
12+
13+
-- The internal state of the component
14+
type ExampleState =
15+
{ counter :: Int
16+
}
17+
18+
-- Create a component by passing a record to the `react` function.
19+
-- The `render` function takes the props and current state, as well as a
20+
-- state update callback, and produces a document.
21+
component :: R.ReactComponent ExampleProps
22+
component = R.react
23+
{ initialState: \_ -> { counter: 0 }
24+
, render: \{ label } { counter } setState ->
25+
R.button { onClick: mkEffFn1 \_ -> do
26+
setState { counter: counter + 1 }
27+
}
28+
[ R.text (label <> ": " <> show counter) ]
29+
}

0 commit comments

Comments
 (0)