• Stars
    star
    351
  • Rank 120,906 (Top 3 %)
  • Language
    Haskell
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React bindings for Haskell

React-Haskell Hackage

As crazy as it seems, using React and Haskell together just may be a good idea.

I was driven to create this thing because I had a large existing Haskell codebase I wanted to put online. However, even without existing code, I think a lot of problems are better modeled in Haskell than JavaScript or other languages. Or you might want to use some existing Haskell libraries.

Examples

Let's put a simple paragraph on the page:

sample :: ReactNode a
sample = p_ [ class_ "style" ] $ em_ "Andy Warhol"

main :: IO ()
main = do
    Just doc <- currentDocument
    let elemId :: JSString
        elemId = "inject"
    Just elem <- documentGetElementById doc elemId
    render sample elem

That creates a DOM node on the page that looks like:

<p class="style">
    <em>Andy Warhol</em>
</p>

We can make that a little more complicated with some more child nodes.

sample :: ReactNode a
sample = div_ [ class_ "beautify" ] $ do
    "The Velvet Underground"

    input_

    "Lou Reed"

But of course that input doesn't do anything. Let's change that.

sample :: JSString -> ReactNode JSString
sample = div_ $ do
    "Favorite artist:"

    input_ [ onChange (Just . value . target) ]

    text str

Getting Started

The first step is a working GHCJS installation. The easiest way is to download a virtual machine with GHCJS pre-installed. I recommend ghcjs-box.

Now that GHCJS is installed we can use cabal to create a project.

$ mkdir project
$ cd project
$ cabal init # generate a .cabal file

Now edit the cabal file to include dependencies.

build-depends:
  base >= 4.8 && < 5,
  ghcjs-base,
  ghcjs-dom,
  react-haskell >= 1.3

Now we can write Main.hs.

sample :: ReactNode a
sample = p_ [ class_ "style" ] $ em_ "Andy Warhol"

main :: IO ()
main = do
    Just elem <- elemById "id"
    render sample elem

Next Steps

Reference

Additional Resources

Is it Right for Me?

React-Haskell is a great tool for building web UI from Haskell. However, you may want to consider the alternatives:

  • By writing plain React / JSX you can speed development by avoiding the GHCJS compilation step. This also has the advantage of being a bit more universal - more people use React through JSX than React-Haskell.
  • ghcjs-react is a very similar project.
  • Reflex is an FRP system built with GHCJS in mind.

Small Print

MIT License

Bitdeli Badge

More Repositories

1

d4

Data-Driven Declarative Documents
JavaScript
818
star
2

react-live-editor

live editing react components
JavaScript
163
star
3

pigment

Programming Language meets UI
JavaScript
51
star
4

slimlock

SLiM + slock = slimlock
C++
39
star
5

evm-rust

Minimal EVM implementation in Rust
Rust
29
star
6

lvca

language verification, construction, and analysis
OCaml
20
star
7

hubble

javascript lenses
JavaScript
15
star
8

haskell-loader

A webpack loader for Haskell (!)
JavaScript
15
star
9

haskell-ipld

An implementation of (some of) IPLD
Haskell
12
star
10

material-ui-hs

Material UI in Haskell!
Haskell
11
star
11

easytest

Simple testing toolkit
Haskell
11
star
12

assimp

Haskell FFI bindings for Assimp
C++
11
star
13

ghcjs-box

Quick start for the vagrant box I use for ghcjs / react-haskell / pigment development
Shell
11
star
14

typed-algebraic-parsing

An implementation of "A Typed, Algebraic Approach to Parsing"
OCaml
10
star
15

daily-typecheckers

I'm writing a typechecker every day
Haskell
9
star
16

mamba-sae

Training and evaluating Sparse Autoencoders for Mamba
Python
9
star
17

simplicity-hs

Implementation of "Simplicity: A New Language for Blockchains"
Haskell
8
star
18

dotfiles

My important config files
Vim Script
6
star
19

Cologne

Good Smelling Ray Tracer
Haskell
6
star
20

jison-loader

jison loader module for webpack
JavaScript
6
star
21

structured-editing

JavaScript
5
star
22

plur

Plurality Monad: Zero, one, or at least two.
Haskell
5
star
23

monaco-ocaml

Ocaml bindings to Microsoft's Monaco editor
JavaScript
4
star
24

lvca-hs

language verification, construction, and automation
Haskell
4
star
25

data-lens-template

Template Haskell utilities for Data.Lens
Haskell
4
star
26

mylens

Haskell
3
star
27

sdl2-image

Haskell bindings to sdl_image for SDL 2
Haskell
3
star
28

gadts

The contents of a talk on GADTs
Haskell
3
star
29

corewarjs

Corewar in Javascript
JavaScript
3
star
30

sdl2-ttf

Haskell bindings for SDL(2)-ttf
Haskell
2
star
31

secret

Haskell
2
star
32

isl-hs

haskell bindings to the integer set library
Haskell
2
star
33

backprop-fun

fun with backprop
Haskell
2
star
34

journal

My public technical journal
Python
2
star
35

berkeley-quorum

Demos / Materials for my Blockchain at Berkeley Developer Course lecture
2
star
36

sdl2-gfx

Haskell bindings to SDL2_gfx
Haskell
2
star
37

turbo-unify

a unification visualization in the browser
Haskell
1
star
38

a-la-carte

Haskell
1
star
39

blog.next

Possibly the technology that will get me blogging again
JavaScript
1
star
40

huttons-raz3r

Haskell
1
star
41

graphreduction

Haskell
1
star
42

tapl

Types and Programming Languages, in Haskell
Haskell
1
star
43

sat

OCaml
1
star
44

hazel

Haskell
1
star
45

formative

My Website
Python
1
star
46

website

The old joelburget.com
JavaScript
1
star
47

diagrams-opengl

An OpenGL Backend for Diagrams
Haskell
1
star
48

joelburget.com

joelburget.com
JavaScript
1
star
49

language-python

Haskell
1
star
50

glowing-ironman

Haskell
1
star
51

benchmarks

OSU RSRG benchmarks website
Python
1
star
52

focusing

Haskell
1
star
53

oplog

Haskell
1
star
54

motivated-list-checking

Haskell
1
star
55

perseus-one-server

An experimental server implementing the Perseus One API
Haskell
1
star
56

symmetric-properties

`Same` / `Unique` monoidal witnesses
Haskell
1
star
57

planetary

playing with programming languages again
Haskell
1
star
58

miftiwac

CSE 731 MIFTIWAC group project
Java
1
star