Skip to content

Commit d40597c

Browse files
authored
Add setup function, to wrap componentDidMount (#10)
* Add setup function, to wrap componentDidMount * Bind this_ * Counter example (no webpack) * Update for new setup function * Add component function, and example * Rename to receiveProps, remove props from initialState * Update readme * Remove package-lock.json
1 parent 833a86d commit d40597c

File tree

19 files changed

+242
-12
lines changed

19 files changed

+242
-12
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This package implements an opinionated set of bindings to the React library, optimizing for the most basic use cases.
44

5-
## Features
5+
## Features
66

77
- All React DOM elements and attributes are supported.
88
- An intuitive API for specifying props - no arrays of key value pairs, just records.
@@ -41,7 +41,8 @@ type ExampleState =
4141
-- state update callback, and produces a document.
4242
example :: R.ReactComponent ExampleProps
4343
example = R.react
44-
{ initialState: \_ -> { counter: 0 }
44+
{ initialState: { counter: 0 }
45+
, receiveProps: \_ _ _ -> pure unit
4546
, render: \{ label } { counter } setState ->
4647
R.button { onClick: mkEffFn1 \_ -> do
4748
setState { counter: counter + 1 }

examples/component/.gitignore

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

examples/component/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 Container output/*/*.js > output/bundle.js
4+
echo 'module.exports = PS.Container;' >> output/bundle.js
5+
node_modules/browserify/bin/cmd.js output/bundle.js index.js -o html/index.js

examples/component/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Component 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/component/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/component/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 Container = require("./output/bundle.js");
6+
7+
ReactDOM.render(
8+
React.createElement(Container.component, { label: 'Increment' }),
9+
document.getElementById("container")
10+
);

examples/component/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "component",
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/component/src/Container.purs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Container where
2+
3+
import Prelude
4+
5+
import React.Basic as R
6+
import ToggleButton as ToggleButton
7+
8+
component :: R.ReactComponent Unit
9+
component = R.react
10+
{ initialState: unit
11+
, receiveProps: \_ _ _ -> pure unit
12+
, render: \_ _ setState ->
13+
R.div { } [ R.component ToggleButton.component { on: true }
14+
, R.component ToggleButton.component { on: false }
15+
]
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module ToggleButton where
2+
3+
import Prelude
4+
5+
import Control.Monad.Eff.Uncurried (mkEffFn1)
6+
import React.Basic as R
7+
8+
type ExampleProps =
9+
{ on :: Boolean
10+
}
11+
12+
type ExampleState =
13+
{ on :: Boolean
14+
}
15+
16+
component :: R.ReactComponent ExampleProps
17+
component = R.react
18+
{ initialState: { on: false }
19+
, receiveProps: \{ on } _ setState -> setState { on }
20+
, render: \_ { on } setState ->
21+
R.button { onClick: mkEffFn1 \_ -> setState { on: not on }
22+
}
23+
[ R.text if on then "On" else "Off" ]
24+
}

examples/counter/.gitignore

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

0 commit comments

Comments
 (0)