Reactive state
Make the window follow your data: create state, read and change it, and know where to write .value.
janelkit Included with janelkit. Nothing extra to install. What you will learn
- Why an ordinary Python variable cannot update the window.
- How state works, and where you write .value and where you do not.
- How lists and dictionaries in state update the window when you change them.
An ordinary variable cannot update the window
When the window is first shown, every {expression} is evaluated and displayed. If you later assign a new value to an ordinary variable, nothing tells the window to look again, so the screen keeps the old value.
state(initial) solves this. It returns a small container that remembers who read it. The binding {count} reads it while being displayed; when the container’s value changes, JanelKit recalculates exactly the bindings that read it and updates them. Reactive means exactly this: the interface follows the data by itself, without code that finds a label and changes its text.
How it works
- A binding is the connection between an expression in the template and the property or text that shows its result.
- Reading state while a binding is evaluated subscribes that binding. Changing the state asks only the subscribed bindings to update.
A counter, line by line
This component keeps a number in state and changes it in two ways: from a Python function and from an action written in the template.
<script lang="python">
from janelkit import state
count = state(0)
def add_one():
count.value += 1
</script>
<window title="Reactive counter" width={420} height={240}>
<column padding={24} gap={12}>
<text>Count: {count}</text>
<text>Doubled: {count * 2}</text>
<button on:click={add_one}>Add one</button>
<button on:click={count = 0}>Reset</button>
</column>
</window>CopyComplete component · Save as src/App.janel in a scaffolded project.
How it works
- count = state(0) creates the container. The name count refers to the container, not to the number.
- In Python, the number is count.value. add_one reads it and writes it back increased by one.
- In the template, count already means the number: {count} and {count * 2} show it and its double.
- on:click={count = 0} is an action in the template. Assigning to a state name there changes its value; it does not replace the container.
What you should see
Count: 0 and Doubled: 0 at first. Each Add one increases both lines (1 and 2, then 2 and 4). Reset sets both back to 0.
The one rule: .value in Python, plain name in the template
In script code, read and write through .value. In the template, write the plain name: JanelKit unwraps it for you. This is the most common source of early errors, so it helps to know where each kind of code lives.
| Where | Read | Change |
|---|---|---|
| Python functions in the script | count.value | count.value = 5, count.value += 1 |
| Template expressions {…} | count | Not possible: expressions only read. |
| Template actions on:click={…} | count | count = 5, count += 1 |
| Other helpers in Python | count.peek() reads without subscribing | count.set(5); count.update(lambda n: n + 1) |
# Ordinary Python: this works in a script or in any module.
from janelkit import state
score = state(10)
score.value = 20 # replace the value
score.set(25) # the same, as a method
score.update(lambda current: current + 5)
assert score.value == 30CopyCommon mistakes
- Writing count = 1 inside a Python function replaces the container with a plain number; the window stops updating. Write count.value = 1.
- Writing {count.value} in the template fails: count is already the number there.
- update expects a function that returns the new value. update(lambda rows: rows.append(x)) stores None, because append returns None.
Lists and dictionaries are reactive too
When state holds a list, a dictionary or a set, it converts it, and anything nested inside, into a reactive version. You can then change it in place: append, remove, sort, item assignment and deletion all update the window, without building a new list.
The conversion makes a copy. Change the collection through items.value, not the original list you passed to state; editing the original has no effect on the window.
<script lang="python">
from janelkit import state
items = state(["Bread", "Milk"])
def add():
items.value.append(f"Item {len(items.value) + 1}")
</script>
<window title="Shopping" width={360} height={280}>
<column padding={24} gap={8}>
<text>{len(items)} items: {", ".join(items)}</text>
<button on:click={add}>Add an item</button>
<button on:click={items.clear()}>Clear</button>
</column>
</window>CopyComplete component · Save as src/App.janel in a scaffolded project.
How it works
- items.value.append(...) changes the list in place; the <text> that reads it updates.
- In the template, items is the list itself, so len(items) and ", ".join(items) work as usual.
- on:click={items.clear()} calls a list method as an action when the button is clicked.
What you should see
2 items: Bread, Milk at first. Add an item adds Item 3, then Item 4. Clear shows 0 items.
Each component has its own state
state created in a component’s script belongs to that instance. If a window shows the same component twice, each copy has its own count. State created in an ordinary Python module, outside a component, is shared by everything that imports it; that is how you share data across components, and it is covered with components.
Common mistakes
- A plain variable is never made reactive just because the template mentions it. Use state for anything that changes while the window is open.
- Do not create new state inside a handler when you mean to update existing state.
Learn moreComponents ↗
Key points
- state(initial) is a container the window watches.
- Python code uses .value; template expressions and actions use the plain name.
- Lists, dictionaries and sets in state update the window when changed in place.
English documentation based on the JanelKit source. For additional details, browse the source docs ↗.