This serves as a quick intro into how to use this framework. First you'll need to learn about the basic primitive which are signals. We create them using
createSignal and they return a read, write pair
1
2
3
4
| import tree
let (read, write) = createSignal(0)
echo read()
write(1) |
This can then be combined with
createEffect to get something that is actually usable.
createEffect will track the signals that it is dependent on and will then rerun when the value of any signal changes (It also runs when initially created, this is so it can find its dependencies)
5
6
7
8
9
10
| createEffect do ():
echo "The value is: ", read()
write(2)
|
Now lets build a TODO app!
Nim code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| import tree
import std/sequtils
type
Todo = ref object
title: string
let (list, setList) = createSignal[seq[Todo]](@[])
proc ItemAdder(): Element =
var inputElem: InputElement
gui:
tdiv:
input(ref inputElem)
button():
proc click() =
setList(list() & Todo(title: $inputElem.value))
"Add TODO"
proc Example(): Element =
gui:
fieldset:
legend: "TODO list"
ItemAdder()
ul:
for idx, item in list():
li:
text(item.title)
proc click(ev: Event) =
var newItems: seq[Todo] = @[]
let currItems = list()
for i in 0 ..< currItems.len:
if i != idx:
newItems &= currItems[i]
setList(newItems) |