|
1 | 1 | # purescript-react-basic
|
| 2 | + |
| 3 | +This package implements an opinionated set of bindings to the React library, optimizing for the most basic use cases. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- All React DOM elements and attributes are supported. |
| 8 | +- An intuitive API for specifying props - no arrays of key value pairs, just records. |
| 9 | +- Attributes are optional, but type-checked. It is a type error to specify `href` as an integer, for example. |
| 10 | + |
| 11 | +## Getting Started |
| 12 | + |
| 13 | +You can install this package using Bower: |
| 14 | + |
| 15 | +``` |
| 16 | +bower install [email protected]:lumihq/purescript-react-basic.git |
| 17 | +``` |
| 18 | + |
| 19 | +Here is an example component which renders a label read from props along with a counter: |
| 20 | + |
| 21 | +```purescript |
| 22 | +module React.Basic.Example where |
| 23 | +
|
| 24 | +import Prelude |
| 25 | +
|
| 26 | +import Data.Function.Uncurried (mkEffFn1) |
| 27 | +import React.Basic as R |
| 28 | +
|
| 29 | +-- The props for the component |
| 30 | +type ExampleProps = |
| 31 | + { label :: String |
| 32 | + } |
| 33 | +
|
| 34 | +-- The internal state of the component |
| 35 | +type ExampleState = |
| 36 | + { counter :: Int |
| 37 | + } |
| 38 | +
|
| 39 | +-- Create a component by passing a record to the `react` function. |
| 40 | +-- The `render` function takes the props and current state, as well as a |
| 41 | +-- state update callback, and produces a document. |
| 42 | +example :: R.ReactComponent ExampleProps |
| 43 | +example = R.react |
| 44 | + { initialState: { counter: 0 } |
| 45 | + , render: \{ label } { counter } setState -> |
| 46 | + R.button { onClick: mkEffFn1 \_ -> do |
| 47 | + setState { counter: counter + 1 } |
| 48 | + } |
| 49 | + [ R.text (label <> ": " <> show counter) ] |
| 50 | + } |
| 51 | +``` |
| 52 | + |
| 53 | +This component can be used directly from JavaScript. For example, if you are using `purs-loader`: |
| 54 | + |
| 55 | +```javascript |
| 56 | +import {example as Example} from 'React.Basic.Example.purs'; |
| 57 | + |
| 58 | +const myComponent = () => ( |
| 59 | + <Example label='Increment' /> |
| 60 | +); |
| 61 | +``` |
0 commit comments