|
1 | | -purescript-react |
2 | | -================ |
| 1 | +# purescript-react |
3 | 2 |
|
4 | | -[](http://github.com/paf31)  |
| 3 | +[](http://github.com/paf31)  |
5 | 4 |
|
6 | 5 | Low-level React Bindings for PureScript. |
7 | 6 |
|
8 | 7 | For a more high-level set of bindings, you might like to look at `purescript-thermite`. |
9 | 8 |
|
10 | 9 | - [Module Documentation](docs/) |
11 | 10 |
|
12 | | -## Dynamic children |
13 | | - |
14 | | -There are two ways that child elements can be passed to components: |
15 | | -- The first way is to spread the child element array when passing them to React's `createElement` function. The [React.DOM](docs/React/DOM.md) and [React.DOM.SVG](docs/React/DOM/SVG.md) spread the child element array. |
16 | | -- The second way is to pass the child element array to `createElement` without spreading. This is useful when dealing with [dynamic children](https://facebook.github.io/react/docs/multiple-components.html#dynamic-children). In this case a `key` property should be assigned direclty to each child. The [React.DOM.Dynamic](docs/React/DOM/Dynamic.md) and [React.DOM.SVG.Dynamic](docs/React/DOM/SVG/Dynamic.md) pass the child element array without spreading. |
17 | | - |
18 | | -Note that this module provides functions `createElement` and `createElementDynamic` to handle the two cases above for component classes. |
19 | | - |
20 | | -## Building |
21 | | - |
22 | | -The library and example can be built with Pulp as follows: |
23 | | - |
24 | | - pulp dep update |
25 | | - pulp build |
26 | | - |
27 | | - pulp test -r cat > example/index.js |
28 | | - open example/index.html |
| 11 | +``` |
| 12 | +bower install purescript-react |
| 13 | +``` |
29 | 14 |
|
30 | 15 | ## Example |
31 | 16 |
|
32 | | -```purescript |
33 | | -module Main where |
34 | | -
|
35 | | -import Prelude |
36 | | -
|
37 | | -import Control.Monad.Eff |
38 | | -
|
39 | | -import Data.Maybe.Unsafe (fromJust) |
40 | | -import Data.Nullable (toMaybe) |
41 | | -
|
42 | | -import DOM (DOM()) |
43 | | -import DOM.HTML (window) |
44 | | -import DOM.HTML.Document (body) |
45 | | -import DOM.HTML.Types (htmlElementToElement) |
46 | | -import DOM.HTML.Window (document) |
47 | | -
|
48 | | -import DOM.Node.Types (Element()) |
49 | | -
|
50 | | -import React |
51 | | -
|
52 | | -import qualified React.DOM as D |
53 | | -import qualified React.DOM.Props as P |
54 | | -
|
55 | | -incrementCounter ctx e = do |
56 | | - val <- readState ctx |
57 | | - writeState ctx (val + 1) |
58 | | -
|
59 | | -counter = createClass $ spec 0 \ctx -> do |
60 | | - val <- readState ctx |
61 | | - return $ D.p [ P.className "Counter" |
62 | | - , P.onClick (incrementCounter ctx) |
63 | | - ] |
64 | | - [ D.text (show val) |
65 | | - , D.text " Click me to increment!" |
66 | | - ] |
67 | | -
|
68 | | -main = container >>= render ui |
69 | | - where |
70 | | - ui :: ReactElement |
71 | | - ui = D.div [] [ createFactory counter {} ] |
72 | | -
|
73 | | - container :: forall eff. Eff (dom :: DOM | eff) Element |
74 | | - container = do |
75 | | - win <- window |
76 | | - doc <- document win |
77 | | - elm <- fromJust <$> toMaybe <$> body doc |
78 | | - return $ htmlElementToElement elm |
79 | | -``` |
| 17 | +Please refer to [purescript-react-example](https://github.com/purescript-contrib/purescript-react) |
0 commit comments