Description
While building marimo dashboards for gprMax (merged in gprMax/gprMax#810), I hit a silent data-loss bug: a list accumulated in mo.state reset to its initial value whenever an unrelated dropdown changed.
The cause was that mo.state([]) lived in a cell that also read the dropdown. Each rerun of that cell calls mo.state() again and gets a fresh State, so the old value is dropped and dependents rerun with []. This matches the implementation, so I don't think it's a runtime bug. But neither the docstring nor the state guide mentions it, and nothing errors or warns. The data just disappears.
Repro (marimo 0.25.1):
import marimo as mo
choice = mo.ui.dropdown(["a", "b"], value="a")
choice
choice.value # any reactive input
get_items, set_items = mo.state([])
add = mo.ui.button(label="Add", on_click=lambda _: set_items(lambda v: v + [len(v)]))
add
Click Add three times to get [0, 1, 2]. Change the dropdown and the list resets to []. Moving mo.state([]) into its own cell with no reactive inputs fixes it.
Proposal:
- A
marimo check rule that flags mo.state() in a cell whose references include anything beyond imports.
- A short note in the state guide that state lives only as long as the cell that created it.
Are you willing to submit a PR? (You must receive approval from the team before submitting a PR.)
Alternatives
Documenting the behavior only, without a lint rule. Cheaper, but the failure is silent, so people are unlikely to find the docs note until after they have lost data.
Additional context
Before fix, dropdown "a": 3 items

Before fix, dropdown "b": reset to 0 items

After fix, dropdown "a": 3 items

After fix, dropdown "b": still 3 items

Description
While building marimo dashboards for gprMax (merged in gprMax/gprMax#810), I hit a silent data-loss bug: a list accumulated in
mo.statereset to its initial value whenever an unrelated dropdown changed.The cause was that
mo.state([])lived in a cell that also read the dropdown. Each rerun of that cell callsmo.state()again and gets a freshState, so the old value is dropped and dependents rerun with[]. This matches the implementation, so I don't think it's a runtime bug. But neither the docstring nor the state guide mentions it, and nothing errors or warns. The data just disappears.Repro (marimo 0.25.1):
get_items()Click Add three times to get
[0, 1, 2]. Change the dropdown and the list resets to[]. Movingmo.state([])into its own cell with no reactive inputs fixes it.Proposal:
marimo checkrule that flagsmo.state()in a cell whose references include anything beyond imports.Are you willing to submit a PR? (You must receive approval from the team before submitting a PR.)
Alternatives
Documenting the behavior only, without a lint rule. Cheaper, but the failure is silent, so people are unlikely to find the docs note until after they have lost data.
Additional context
Before fix, dropdown "a": 3 items


Before fix, dropdown "b": reset to 0 items
After fix, dropdown "a": 3 items


After fix, dropdown "b": still 3 items