Skip to content

Commit 28f1cba

Browse files
committed
Merge branch 'phil/setup' into phil/counter
2 parents 5c44118 + 2806dfb commit 28f1cba

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
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.
@@ -42,6 +42,7 @@ type ExampleState =
4242
example :: R.ReactComponent ExampleProps
4343
example = R.react
4444
{ initialState: \_ -> { counter: 0 }
45+
, setup: \_ _ _ -> pure unit
4546
, render: \{ label } { counter } setState ->
4647
R.button { onClick: mkEffFn1 \_ -> do
4748
setState { counter: counter + 1 }

generated-docs/React/Basic.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#### `react`
44

55
``` purescript
6-
react :: forall props state. { initialState :: state, render :: props -> state -> (state -> Eff (react :: ReactFX) Unit) -> JSX } -> ReactComponent props
6+
react :: forall props state. { initialState :: props -> state, setup :: props -> state -> (state -> Eff (react :: ReactFX) Unit) -> Eff (react :: ReactFX) Unit, render :: props -> state -> (state -> Eff (react :: ReactFX) Unit) -> JSX } -> ReactComponent props
77
```
88

99
Create a React component from a _specification_ of that component.

src/React/Basic.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ exports.react_ = function(spec) {
77
getInitialState: function() {
88
return spec.initialState(this.props);
99
},
10+
componentDidMount: function() {
11+
var this_ = this;
12+
spec.setup(this.props, this.state, function(newState) {
13+
return function() {
14+
this_.setState(newState);
15+
};
16+
});
17+
},
1018
render: function() {
1119
var this_ = this;
1220
return spec.render(this.props, this.state, function(newState) {

src/React/Basic.purs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module React.Basic
77
import Prelude
88

99
import Control.Monad.Eff (Eff, kind Effect)
10+
import Control.Monad.Eff.Uncurried (EffFn3, mkEffFn3)
1011
import Data.Function.Uncurried (Fn3, mkFn3)
1112
import React.Basic.DOM as React.Basic.DOM
1213
import React.Basic.Types (CSS, EventHandler, JSX, ReactComponent, ReactFX)
@@ -15,6 +16,7 @@ import React.Basic.Types as React.Basic.Types
1516
foreign import react_
1617
:: forall props state
1718
. { initialState :: props -> state
19+
, setup :: EffFn3 (react :: ReactFX) props state (state -> Eff (react :: ReactFX) Unit) Unit
1820
, render :: Fn3 props state (state -> Eff (react :: ReactFX) Unit) JSX
1921
}
2022
-> ReactComponent props
@@ -31,7 +33,13 @@ foreign import react_
3133
react
3234
:: forall props state
3335
. { initialState :: props -> state
36+
, setup :: props -> state -> (state -> Eff (react :: ReactFX) Unit) -> Eff (react :: ReactFX) Unit
3437
, render :: props -> state -> (state -> Eff (react :: ReactFX) Unit) -> JSX
3538
}
3639
-> ReactComponent props
37-
react { initialState, render } = react_ { initialState, render: mkFn3 render }
40+
react { initialState, setup, render } =
41+
react_
42+
{ initialState
43+
, setup: mkEffFn3 setup
44+
, render: mkFn3 render
45+
}

0 commit comments

Comments
 (0)