Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit af7275e

Browse files
committed
Update README.md. Make view functions. Add using macro to R module
1 parent b9e3b10 commit af7275e

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Elixirscript React Example
22

3+
This project shows an example of using Elixirscript with React.
4+
5+
There is an Elixir module ,`R`, defined at `app/elixirscript/r.exjs` that creates macros that create React elements.
6+
7+
The module, `App`, defined at `app/elixirscript/app.exjs` uses these macros to create React elements thanks to React's ability to make stateless components. The rest of the module shows something
8+
similar to the Elm architecture: Model, View, Update.
9+
10+
The model here is just a map with name and email fields. The model is saved within an Agent.
11+
12+
The update function is responsible for taking messages and using them to update the model. At the very end, it renders the new view.
13+
14+
The view is a function that creates HTML via React components. In the example, it also defines helper view functions to compose together the main view. This is less typical of something you would see in React and more like the patterns seen in Elm.
15+
316
## Requirements
417

518
Must have [elixirscript](https://github.com/bryanjos/elixirscript) installed.

app/elixirscript/app.exjs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
defmodule App do
22
@on_js_load :main
33
require JS
4-
JS.import React, "react"
5-
JS.import ReactDOM, "react-dom"
6-
import R
4+
use R
75

86
def initial_state() do
97
%{name: "", email: ""}
@@ -22,19 +20,25 @@ defmodule App do
2220
render()
2321
end
2422

23+
def my_form(model) do
24+
form do
25+
input [type: "text", placeholder: "Name", onChange: &update(&1, :name), value: model.name]
26+
input [type: "email", placeholder: "Email", onChange: &update(&1, :email), value: model.email]
27+
end
28+
end
29+
30+
def field(title, value) do
31+
div do
32+
"#{title}: #{value}"
33+
end
34+
end
35+
2536
def view(model) do
26-
div do
27-
form do
28-
input [type: "text", placeholder: "Name", onChange: &update(&1, :name), value: model.name]
29-
input [type: "email", placeholder: "Email", onChange: &update(&1, :email), value: model.email]
30-
end
31-
div do
32-
"Name: #{model.name}"
33-
end
34-
div do
35-
"Email: #{model.email}"
36-
end
37-
end
37+
div do
38+
my_form(model)
39+
field("Name", model.name)
40+
field("Email", model.email)
41+
end
3842
end
3943

4044
def render() do

app/elixirscript/r.exjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
defmodule R do
22
@load_only true
33

4+
defmacro __using__(_) do
5+
quote do
6+
JS.import React, "react"
7+
JS.import ReactDOM, "react-dom"
8+
import R
9+
end
10+
end
11+
412
@external_resource tags_path = Path.join([__DIR__, "tags.txt"])
513

614
@tags (for line <- File.stream!(tags_path, [], :line) do

0 commit comments

Comments
 (0)