GUIDE / FUNDAMENTALS

Reactive state

Make the window follow your data: create state, read and change it, and know where to write .value.

Core janelkit Included with janelkit. Nothing extra to install.
CHAPTER 06 / 29~3 min read · try examples at your own pace
Alpha / 0.2 — These pages document JanelKit 0.2 and its packages. Each series (0.1, 0.2…) may change the public API, always with migration notes: see Changes and migration. The website uses Svelte; the desktop examples use Python and JanelKit, not JavaScript.

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

  1. A binding is the connection between an expression in the template and the property or text that shows its result.
  2. 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.

janel
<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>
Copy

Complete component · Save as src/App.janel in a scaffolded project.

How it works

  1. count = state(0) creates the container. The name count refers to the container, not to the number.
  2. In Python, the number is count.value. add_one reads it and writes it back increased by one.
  3. In the template, count already means the number: {count} and {count * 2} show it and its double.
  4. 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.

WhereReadChange
Python functions in the scriptcount.valuecount.value = 5, count.value += 1
Template expressions {…}countNot possible: expressions only read.
Template actions on:click={…}countcount = 5, count += 1
Other helpers in Pythoncount.peek() reads without subscribingcount.set(5); count.update(lambda n: n + 1)
python
# 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 == 30
Copy

Common 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.

janel
<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>
Copy

Complete component · Save as src/App.janel in a scaffolded project.

How it works

  1. items.value.append(...) changes the list in place; the <text> that reads it updates.
  2. In the template, items is the list itself, so len(items) and ", ".join(items) work as usual.
  3. 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.

Learn moreConditions and lists ↗Reactivity in depth ↗

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 ↗.