# State and data flow

> CrossFly's state philosophy — no reactive binding, no DI, imperative + explicit invalidation.

Source: https://crossfly.dev/docs/concepts/state-and-data-flow — CrossFly documentation. Full docs as one file: https://crossfly.dev/llms-full.txt

# State and data flow

This is where CrossFly most surprises those coming from Flutter, Compose, SwiftUI, or React. Two deliberate
absences:

- **There is no automatic reactive binding.** No `setState`, observables, `@State`, or recomposition. Changing
  the model does **not** update the UI on its own.
- **There is no dependency injection container.** No Dagger/Guice/Spring. You build and pass your
  dependencies by hand (typically through the screen's constructor).

## The model: imperative + explicit invalidation
You **read/write the model**, **update the component directly**, and the framework **repaints**:
```java
model.nome = "Ana";
label.setText(model.nome);   // most setters already invalidate the layout and call repaint()
```
Most component setters already fire `repaint()`/invalidate the layout for you. The single
rule to remember:

> **Changed state the framework doesn't know about** (a counter of yours, a computation, a field drawn in
> `onPaint`)? **You** call `repaint()`. (See [The paint thread](/docs/concepts/the-paint-thread).)

### The classic counterexample
```java
this.progresso = 0.7f;   // I changed a field that only my onPaint reads…
// …and the screen doesn't change. Missing:
Component.repaint();      // ✔ now the next frame reflects the new value
```
"I changed the state and the screen didn't update" is, almost always, a missing `repaint()`.

## Why this choice
- **Predictability:** the flow is linear and visible — data → setter → repaint → frame. There is no
  magic recomposition nor hidden reactive cycle to debug.
- **Zero magic, low cost:** no reactive dependency graph, no DI framework to configure.
- The price is explicitness: **managing state is the application's responsibility**, not the framework's.

## The communication mechanisms the framework offers
- **Callbacks / actions** — `onAction(...)`, callbacks passed through the constructor (see
  [Returning results](/docs/ui/navigation/returning-results)).
- **Parent/child hierarchy** — navigate the component tree.
- **`property(k, v)`** — the per-component "data bag" (see [Everything is a Component](/docs/concepts/everything-is-a-component)).
- **Global state** — static fields/shared services when it makes sense.

## The recommended pattern
Since the framework only provides the building blocks, the organization pattern (a **ViewModel/controller**
per screen + **repository** for data) is an application convention — documented in
[best-practices](/docs/best-practices). The framework does not impose an architecture; it offers
components and the setter→`repaint()` pair.

<div class="cf-diagram">
<svg role="img" id="my-svg" width="967" height="78" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="flowchart" style="max-width: 966.969px; background-color: transparent;" viewBox="0 0.01342010498046875 966.96875 77.98657989501953" role="graphics-document document" aria-roledescription="flowchart-v2"><style>#my-svg{font-family:Manrope,system-ui,sans-serif;font-size:14px;fill:var(--text);}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#my-svg .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#my-svg .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#my-svg .error-icon{fill:var(--bg-elev);}#my-svg .error-text{fill:var(--text-dim);stroke:var(--text-dim);}#my-svg .edge-thickness-normal{stroke-width:1px;}#my-svg .edge-thickness-thick{stroke-width:3.5px;}#my-svg .edge-pattern-solid{stroke-dasharray:0;}#my-svg .edge-thickness-invisible{stroke-width:0;fill:none;}#my-svg .edge-pattern-dashed{stroke-dasharray:3;}#my-svg .edge-pattern-dotted{stroke-dasharray:2;}#my-svg .marker{fill:var(--text-dim);stroke:var(--text-dim);}#my-svg .marker.cross{stroke:var(--text-dim);}#my-svg svg{font-family:Manrope,system-ui,sans-serif;font-size:14px;}#my-svg p{margin:0;}#my-svg .label{font-family:Manrope,system-ui,sans-serif;color:var(--text);}#my-svg .cluster-label text{fill:var(--text-dim);}#my-svg .cluster-label span{color:var(--text-dim);}#my-svg .cluster-label span p{background-color:transparent;}#my-svg .label text,#my-svg span{fill:var(--text);color:var(--text);}#my-svg .node rect,#my-svg .node circle,#my-svg .node ellipse,#my-svg .node polygon,#my-svg .node path{fill:var(--bg-soft);stroke:var(--border-strong);stroke-width:1px;}#my-svg .rough-node .label text,#my-svg .node .label text,#my-svg .image-shape .label,#my-svg .icon-shape .label{text-anchor:middle;}#my-svg .node .katex path{fill:var(--text-dim);stroke:var(--text-dim);stroke-width:1px;}#my-svg .rough-node .label,#my-svg .node .label,#my-svg .image-shape .label,#my-svg .icon-shape .label{text-align:center;}#my-svg .node.clickable{cursor:pointer;}#my-svg .root .anchor path{fill:var(--text-dim)!important;stroke-width:0;stroke:var(--text-dim);}#my-svg .arrowheadPath{fill:rgba(255, 255, 255, 0);}#my-svg .edgePath .path{stroke:var(--text-dim);stroke-width:1px;}#my-svg .flowchart-link{stroke:var(--text-dim);fill:none;}#my-svg .edgeLabel{background-color:var(--bg-elev);text-align:center;}#my-svg .edgeLabel p{background-color:var(--bg-elev);}#my-svg .edgeLabel rect{opacity:0.5;background-color:var(--bg-elev);fill:var(--bg-elev);}#my-svg .labelBkg{background-color:rgba(255, 255, 255, 0.5);}#my-svg .cluster rect{fill:var(--bg-elev);stroke:hsl(0, 0%, 90%);stroke-width:1px;}#my-svg .cluster text{fill:var(--text-dim);}#my-svg .cluster span{color:var(--text-dim);}#my-svg div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:Manrope,system-ui,sans-serif;font-size:12px;background:var(--bg-elev);border:1px solid hsl(0, 0%, 90%);border-radius:2px;pointer-events:none;z-index:100;}#my-svg .flowchartTitleText{text-anchor:middle;font-size:18px;fill:var(--text);}#my-svg rect.text{fill:none;stroke-width:0;}#my-svg .icon-shape,#my-svg .image-shape{background-color:var(--bg-elev);text-align:center;}#my-svg .icon-shape p,#my-svg .image-shape p{background-color:var(--bg-elev);padding:2px;}#my-svg .icon-shape .label rect,#my-svg .image-shape .label rect{opacity:0.5;background-color:var(--bg-elev);fill:var(--bg-elev);}#my-svg .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#my-svg .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#my-svg .node .neo-node{stroke:var(--border-strong);}#my-svg [data-look="neo"].node rect,#my-svg [data-look="neo"].cluster rect,#my-svg [data-look="neo"].node polygon{stroke:url(#my-svg-gradient);filter:drop-shadow( 1px 2px 2px rgba(185,185,185,1));}#my-svg [data-look="neo"].swimlane.cluster rect{filter:none;}#my-svg [data-look="neo"].node path{stroke:url(#my-svg-gradient);stroke-width:1px;}#my-svg [data-look="neo"].node .outer-path{filter:drop-shadow( 1px 2px 2px rgba(185,185,185,1));}#my-svg [data-look="neo"].node .neo-line path{stroke:var(--border-strong);filter:none;}#my-svg [data-look="neo"].node circle{stroke:url(#my-svg-gradient);filter:drop-shadow( 1px 2px 2px rgba(185,185,185,1));}#my-svg [data-look="neo"].node circle .state-start{fill:var(--text-dim);}#my-svg [data-look="neo"].icon-shape .icon{fill:url(#my-svg-gradient);filter:drop-shadow( 1px 2px 2px rgba(185,185,185,1));}#my-svg [data-look="neo"].icon-shape .icon-neo path{stroke:url(#my-svg-gradient);filter:drop-shadow( 1px 2px 2px rgba(185,185,185,1));}#my-svg :root{--mermaid-font-family:Manrope,system-ui,sans-serif;}</style><g><marker id="my-svg_flowchart-v2-pointEnd" class="marker flowchart-v2" viewBox="0 0 10 10" refX="5" refY="5" markerUnits="userSpaceOnUse" markerWidth="8" markerHeight="8" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowMarkerPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-pointStart" class="marker flowchart-v2" viewBox="0 0 10 10" refX="4.5" refY="5" markerUnits="userSpaceOnUse" markerWidth="8" markerHeight="8" orient="auto"><path d="M 0 5 L 10 10 L 10 0 z" class="arrowMarkerPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-pointEnd-margin" class="marker flowchart-v2" viewBox="0 0 11.5 14" refX="11.5" refY="7" markerUnits="userSpaceOnUse" markerWidth="10.5" markerHeight="14" orient="auto"><path d="M 0 0 L 11.5 7 L 0 14 z" class="arrowMarkerPath" style="stroke-width: 0; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-pointStart-margin" class="marker flowchart-v2" viewBox="0 0 11.5 14" refX="1" refY="7" markerUnits="userSpaceOnUse" markerWidth="11.5" markerHeight="14" orient="auto"><polygon points="0,7 11.5,14 11.5,0" class="arrowMarkerPath" style="stroke-width: 0; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-circleEnd" class="marker flowchart-v2" viewBox="0 0 10 10" refX="11" refY="5" markerUnits="userSpaceOnUse" markerWidth="11" markerHeight="11" orient="auto"><circle cx="5" cy="5" r="5" class="arrowMarkerPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-circleStart" class="marker flowchart-v2" viewBox="0 0 10 10" refX="-1" refY="5" markerUnits="userSpaceOnUse" markerWidth="11" markerHeight="11" orient="auto"><circle cx="5" cy="5" r="5" class="arrowMarkerPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-circleEnd-margin" class="marker flowchart-v2" viewBox="0 0 10 10" refY="5" refX="12.25" markerUnits="userSpaceOnUse" markerWidth="14" markerHeight="14" orient="auto"><circle cx="5" cy="5" r="5" class="arrowMarkerPath" style="stroke-width: 0; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-circleStart-margin" class="marker flowchart-v2" viewBox="0 0 10 10" refX="-2" refY="5" markerUnits="userSpaceOnUse" markerWidth="14" markerHeight="14" orient="auto"><circle cx="5" cy="5" r="5" class="arrowMarkerPath" style="stroke-width: 0; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-crossEnd" class="marker cross flowchart-v2" viewBox="0 0 11 11" refX="12" refY="5.2" markerUnits="userSpaceOnUse" markerWidth="11" markerHeight="11" orient="auto"><path d="M 1,1 l 9,9 M 10,1 l -9,9" class="arrowMarkerPath" style="stroke-width: 2; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-crossStart" class="marker cross flowchart-v2" viewBox="0 0 11 11" refX="-1" refY="5.2" markerUnits="userSpaceOnUse" markerWidth="11" markerHeight="11" orient="auto"><path d="M 1,1 l 9,9 M 10,1 l -9,9" class="arrowMarkerPath" style="stroke-width: 2; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-crossEnd-margin" class="marker cross flowchart-v2" viewBox="0 0 15 15" refX="17.7" refY="7.5" markerUnits="userSpaceOnUse" markerWidth="12" markerHeight="12" orient="auto"><path d="M 1,1 L 14,14 M 1,14 L 14,1" class="arrowMarkerPath" style="stroke-width: 2.5;"/></marker><marker id="my-svg_flowchart-v2-crossStart-margin" class="marker cross flowchart-v2" viewBox="0 0 15 15" refX="-3.5" refY="7.5" markerUnits="userSpaceOnUse" markerWidth="12" markerHeight="12" orient="auto"><path d="M 1,1 L 14,14 M 1,14 L 14,1" class="arrowMarkerPath" style="stroke-width: 2.5; stroke-dasharray: 1, 0;"/></marker><g class="root"><g class="clusters"/><g class="edgePaths"><path d="M181.016,39L188.185,39C195.354,39,209.693,39,223.365,39C237.036,39,250.042,39,256.544,39L263.047,39" id="my-svg-L_Model_VM_0" class="edge-thickness-normal edge-pattern-solid edge-thickness-normal edge-pattern-solid flowchart-link" style=";" data-edge="true" data-et="edge" data-id="L_Model_VM_0" data-points="W3sieCI6MTgxLjAxNTYyNSwieSI6Mzl9LHsieCI6MjI0LjAzMTI1LCJ5IjozOX0seyJ4IjoyNjcuMDQ2ODc1LCJ5IjozOX1d" data-look="classic" marker-end="url(#my-svg_flowchart-v2-pointEnd)"/><path d="M443.797,28.357L457.438,26.714C471.078,25.071,498.359,21.786,524.98,21.947C551.601,22.108,577.562,25.715,590.542,27.519L603.522,29.323" id="my-svg-L_VM_Comp_0" class="edge-thickness-normal edge-pattern-solid edge-thickness-normal edge-pattern-solid flowchart-link" style=";" data-edge="true" data-et="edge" data-id="L_VM_Comp_0" data-points="W3sieCI6NDQzLjc5Njg3NSwieSI6MjguMzU2NzEwMTE1NjU5OTk3fSx7IngiOjUyNS42NDA2MjUsInkiOjE4LjV9LHsieCI6NjA3LjQ4NDM3NSwieSI6MjkuODczMDg2OTAzNTY0MDV9XQ==" data-look="classic" marker-end="url(#my-svg_flowchart-v2-pointEnd)"/><path d="M738.844,39L746.766,39C754.688,39,770.531,39,785.708,39C800.885,39,815.396,39,822.651,39L829.906,39" id="my-svg-L_Comp_Frame_0" class="edge-thickness-normal edge-pattern-solid edge-thickness-normal edge-pattern-solid flowchart-link" style=";" data-edge="true" data-et="edge" data-id="L_Comp_Frame_0" data-points="W3sieCI6NzM4Ljg0Mzc1LCJ5IjozOX0seyJ4Ijo3ODYuMzc1LCJ5IjozOX0seyJ4Ijo4MzMuOTA2MjUsInkiOjM5fV0=" data-look="classic" marker-end="url(#my-svg_flowchart-v2-pointEnd)"/><path d="M607.484,48.127L593.844,50.022C580.203,51.918,552.922,55.709,526.303,56.041C499.683,56.374,473.726,53.248,460.747,51.685L447.768,50.122" id="my-svg-L_Comp_VM_0" class="edge-thickness-normal edge-pattern-solid edge-thickness-normal edge-pattern-solid flowchart-link" style=";" data-edge="true" data-et="edge" data-id="L_Comp_VM_0" data-points="W3sieCI6NjA3LjQ4NDM3NSwieSI6NDguMTI2OTEzMDk2NDM1OTV9LHsieCI6NTI1LjY0MDYyNSwieSI6NTkuNX0seyJ4Ijo0NDMuNzk2ODc1LCJ5Ijo0OS42NDMyODk4ODQzNDAwMX1d" data-look="classic" marker-end="url(#my-svg_flowchart-v2-pointEnd)"/></g><g class="edgeLabels"><g class="edgeLabel" transform="translate(224.03125, 39)"><g class="label" data-id="L_Model_VM_0" transform="translate(-18.015625, -10.5)"><foreignObject width="36.03125" height="21"><div xmlns="http://www.w3.org/1999/xhtml" class="labelBkg" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="edgeLabel"><p>reads</p></span></div></foreignObject></g></g><g class="edgeLabel" transform="translate(525.73721, 18.51342)"><g class="label" data-id="L_VM_Comp_0" transform="translate(-56.84375, -10.5)"><foreignObject width="113.6875" height="21"><div xmlns="http://www.w3.org/1999/xhtml" class="labelBkg" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="edgeLabel"><p>setText / setItems</p></span></div></foreignObject></g></g><g class="edgeLabel" transform="translate(786.375, 39)"><g class="label" data-id="L_Comp_Frame_0" transform="translate(-22.53125, -10.5)"><foreignObject width="45.0625" height="21"><div xmlns="http://www.w3.org/1999/xhtml" class="labelBkg" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="edgeLabel"><p>repaint</p></span></div></foreignObject></g></g><g class="edgeLabel" transform="translate(525.640625, 59.5)"><g class="label" data-id="L_Comp_VM_0" transform="translate(-28.609375, -10.5)"><foreignObject width="57.21875" height="21"><div xmlns="http://www.w3.org/1999/xhtml" class="labelBkg" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="edgeLabel"><p>onAction</p></span></div></foreignObject></g></g></g><g class="nodes"><g class="node default" id="my-svg-flowchart-Model-0" data-look="classic" transform="translate(94.5078125, 39)"><rect class="basic label-container" style="" x="-86.5078125" y="-24.5" width="173.015625" height="49"/><g class="label" style="" transform="translate(-58.5078125, -10.5)"><rect/><foreignObject width="117.015625" height="21"><div xmlns="http://www.w3.org/1999/xhtml" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="nodeLabel"><p>model / repository</p></span></div></foreignObject></g></g><g class="node default" id="my-svg-flowchart-VM-1" data-look="classic" transform="translate(355.421875, 39)"><rect class="basic label-container" style="" x="-88.375" y="-24.5" width="176.75" height="49"/><g class="label" style="" transform="translate(-60.375, -10.5)"><rect/><foreignObject width="120.75" height="21"><div xmlns="http://www.w3.org/1999/xhtml" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="nodeLabel"><p>ViewModel (yours)</p></span></div></foreignObject></g></g><g class="node default" id="my-svg-flowchart-Comp-3" data-look="classic" transform="translate(673.1640625, 39)"><rect class="basic label-container" style="" x="-65.6796875" y="-24.5" width="131.359375" height="49"/><g class="label" style="" transform="translate(-37.6796875, -10.5)"><rect/><foreignObject width="75.359375" height="21"><div xmlns="http://www.w3.org/1999/xhtml" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="nodeLabel"><p>Component</p></span></div></foreignObject></g></g><g class="node default" id="my-svg-flowchart-Frame-5" data-look="classic" transform="translate(896.4375, 39)"><rect class="basic label-container" style="" x="-62.53125" y="-24.5" width="125.0625" height="49"/><g class="label" style="" transform="translate(-34.53125, -10.5)"><rect/><foreignObject width="69.0625" height="21"><div xmlns="http://www.w3.org/1999/xhtml" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="nodeLabel"><p>next frame</p></span></div></foreignObject></g></g></g></g></g><defs><filter id="my-svg-drop-shadow" height="130%" width="130%"><feDropShadow dx="4" dy="4" stdDeviation="0" flood-opacity="0.06" flood-color="var(--text-dim)"/></filter></defs><defs><filter id="my-svg-drop-shadow-small" height="150%" width="150%"><feDropShadow dx="2" dy="2" stdDeviation="0" flood-opacity="0.06" flood-color="var(--text-dim)"/></filter></defs><linearGradient id="my-svg-gradient" gradientUnits="objectBoundingBox" x1="0%" y1="0%" x2="100%" y2="0%"><stop offset="0%" stop-color="var(--border-strong)" stop-opacity="1"/><stop offset="100%" stop-color="hsl(0, 0%, 90%)" stop-opacity="1"/></linearGradient></svg>
</div>

## See also
- Related: [The paint thread](/docs/concepts/the-paint-thread) · [App architecture (best-practices)](/docs/best-practices)
