|
1 | 1 | module React.Basic
|
2 | 2 | ( react
|
| 3 | + , component |
3 | 4 | , module React.Basic.DOM
|
4 | 5 | , module React.Basic.Types
|
5 | 6 | ) where
|
6 | 7 |
|
7 | 8 | import Prelude
|
8 | 9 |
|
9 | 10 | import Control.Monad.Eff (Eff, kind Effect)
|
10 |
| -import Data.Function.Uncurried (Fn3, mkFn3) |
| 11 | +import Control.Monad.Eff.Uncurried (EffFn3, mkEffFn3) |
| 12 | +import Data.Function.Uncurried (Fn2, runFn2, Fn3, mkFn3) |
11 | 13 | import React.Basic.DOM as React.Basic.DOM
|
12 | 14 | import React.Basic.Types (CSS, EventHandler, JSX, ReactComponent, ReactFX)
|
13 | 15 | import React.Basic.Types as React.Basic.Types
|
14 | 16 |
|
15 | 17 | foreign import react_
|
16 | 18 | :: forall props state
|
17 |
| - . { initialState :: props -> state |
| 19 | + . { initialState :: state |
| 20 | + , receiveProps :: EffFn3 (react :: ReactFX) props state (state -> Eff (react :: ReactFX) Unit) Unit |
18 | 21 | , render :: Fn3 props state (state -> Eff (react :: ReactFX) Unit) JSX
|
19 | 22 | }
|
20 | 23 | -> ReactComponent props
|
21 | 24 |
|
22 | 25 | -- | Create a React component from a _specification_ of that component.
|
23 | 26 | -- |
|
24 | 27 | -- | A _specification_ consists of a state type, an initial value for that state,
|
25 |
| --- | and a rendering function which takes a value of that state type, additional |
26 |
| --- | _props_ (which will be passed in by the user) and a state update function. |
| 28 | +-- | a function to apply incoming props to the internal state, and a rendering |
| 29 | +-- | function which takes props, state and a state update function. |
27 | 30 | -- |
|
28 | 31 | -- | The rendering function should return a value of type `JSX`, which can be
|
29 | 32 | -- | constructed using the helper functions provided by the `React.Basic.DOM`
|
30 | 33 | -- | module (and re-exported here).
|
31 | 34 | react
|
32 | 35 | :: forall props state
|
33 |
| - . { initialState :: props -> state |
| 36 | + . { initialState :: state |
| 37 | + , receiveProps :: props -> state -> (state -> Eff (react :: ReactFX) Unit) -> Eff (react :: ReactFX) Unit |
34 | 38 | , render :: props -> state -> (state -> Eff (react :: ReactFX) Unit) -> JSX
|
35 | 39 | }
|
36 | 40 | -> ReactComponent props
|
37 |
| -react { initialState, render } = react_ { initialState, render: mkFn3 render } |
| 41 | +react { initialState, receiveProps, render } = |
| 42 | + react_ |
| 43 | + { initialState |
| 44 | + , receiveProps: mkEffFn3 receiveProps |
| 45 | + , render: mkFn3 render |
| 46 | + } |
| 47 | + |
| 48 | +foreign import component_ :: forall props. Fn2 (ReactComponent props) props JSX |
| 49 | + |
| 50 | +-- | Create a `JSX` node from another React component, by providing the props. |
| 51 | +component |
| 52 | + :: forall props |
| 53 | + . ReactComponent props |
| 54 | + -> props |
| 55 | + -> JSX |
| 56 | +component = runFn2 component_ |
0 commit comments