Sharing data
Choose between props, a shared Python module and context to give several components access to the same data.
janelkit Included with janelkit. Nothing extra to install. What you will learn
- Three ways to share data, and when each one fits.
- How a Python module holds state for the whole application.
- How provide and inject share a value with one part of the tree.
Three ways to share, three different reaches
Props reach one child, explicitly. A Python module reaches the whole application: every component that imports it sees the same objects, because Python imports a module only once. Context reaches the descendants of one component, which is useful when the value depends on where a component sits, such as the current project in one of several panels.
| Mechanism | Reach | Use it for |
|---|---|---|
| Prop | One child | A direct, visible input. The default choice. |
| Python module with state | The whole application | A data service, the shared values of a small app, objects with state of their own. |
| A store (janelkit-store) | The whole application, every window | The application’s data: the user, the documents, a cart. Actions, change tracking and a fresh instance in each test. |
| provide / inject | Descendants of one component | A value that depends on position in the tree, without passing it through every level. |
A shared module: one source of truth for the whole app
Put state and the functions that change it in an ordinary module under src/services. Every component that imports it reads and changes the same data, and every binding that reads it updates. The module has no interface, so it is also easy to test with plain Python.
from janelkit import state
items = state([])
def add(name):
items.value.append(name)
def clear():
items.value.clear()Copy<script lang="python">
from services.cart import items
</script>
<text>Cart: {len(items)} item(s)</text>Copy<script lang="python">
from services import cart
from services.cart import items
from components.CartBadge import CartBadge
</script>
<window title="Shop" width={420} height={260}>
<column padding={24} gap={12}>
<CartBadge />
<row gap={8}>
<button on:click={cart.add("Apple")}>Add an apple</button>
<button on:click={cart.clear()}>Empty the cart</button>
</row>
<text>{", ".join(items) or "The cart is empty."}</text>
</column>
</window>CopyComplete example in 3 files · Save each one at the path shown above it.
How it works
- cart.py creates items once, the first time it is imported. Both components get the same object.
- CartBadge only reads; App changes the cart through the module’s functions, so the rules for changing it live in one place.
- Import the state by name (from services.cart import items) to use it in templates without .value: templates unwrap names, not attributes.
What you should see
Cart: 0 item(s) and The cart is empty. at first. Each Add an apple updates both lines: Cart: 1 item(s) and Apple. Empty the cart resets both.
Common mistakes
- Module state lives as long as the Python process. In tests that mount the app several times in one process, it is not reset between mounts; give the module a reset function, or use a store, whose use() gives each mount a fresh instance.
- An effect or timer created at the top level of a module has no component owner; stop it yourself.
Learn morejanelkit-store ↗
Context: share with one part of the tree
provide(key, value) makes a value available to every descendant of the current component. inject(key) in any descendant, at any depth, returns the value from the nearest ancestor that provides it. Without a provider it raises LookupError; inject(key, default) returns the default instead. Both are called during setup.
The value is shared as is. To make it reactive, share a state, or an object that contains states, not a plain string.
<script lang="python">
from janelkit import inject
workspace = inject("workspace")
</script>
<text>Workspace: {workspace}</text>Copy<script lang="python">
from janelkit import provide, state
from components.WorkspaceBadge import WorkspaceBadge
workspace = state("Studio")
provide("workspace", workspace)
</script>
<window title="Context" width={380} height={200}>
<column padding={24} gap={12}>
<WorkspaceBadge />
<button on:click={workspace = "Lab"}>Change workspace</button>
</column>
</window>CopyComplete example in 2 files · Save each one at the path shown above it.
How it works
- App provides the state object itself, under the key "workspace".
- WorkspaceBadge injects that same object, so its text follows the changes.
- Nothing in between had to pass the value as a prop.
What you should see
Workspace: Studio at first; Workspace: Lab after the click.
Override context for one subtree
A component in the middle can provide the same key with another value. Its descendants receive the nearer value, while the rest of the tree keeps the original. That is how two panels can show two different projects with the same components. A secondary window inherits context from the component that opens it, and the pages of a janelkit-router stack inherit it from the component that creates the stack.
# Script of a middle component, such as a preview panel.
from janelkit import provide, state
preview_workspace = state("Preview")
provide("workspace", preview_workspace) # its descendants now see "Preview"CopyCommon mistakes
- inject chooses the value once, during setup. Change the contents of the shared state, not which object is provided.
- Use a default only for optional values. A missing required provider should fail loudly, so the error points at the real mistake.
Key points
- Prefer props; use a store or a module for application-wide data; use context for data tied to a subtree.
- A module with state is one shared source of truth; change it through its functions.
- provide and inject share the object itself; share state to keep it reactive.
English documentation based on the JanelKit source. For additional details, browse the source docs ↗.