# Estado y flujo de datos

> La filosofía de estado de CrossFly — sin binding reactivo, sin DI, imperativo + invalidación explícita.

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

# Estado y flujo de datos

Es aquí donde CrossFly más sorprende a quien viene de Flutter, Compose, SwiftUI o React. Dos ausencias
deliberadas:

- **No hay binding reactivo automático.** Nada de `setState`, observables, `@State` ni recomposición. Cambiar
  el modelo **no** actualiza la UI por sí solo.
- **No hay contenedor de inyección de dependencias.** Nada de Dagger/Guice/Spring. Tú construyes y pasas tus
  dependencias a mano (típicamente por el constructor de la pantalla).

## El modelo: imperativo + invalidación explícita
Tú **lees/escribes el modelo**, **actualizas el componente directamente** y el framework **repinta**:
```java
model.nome = "Ana";
label.setText(model.nome);   // la mayoría de los setters ya invalida el layout y llama a repaint()
```
La mayor parte de los setters de componente ya dispara `repaint()`/invalida el layout por ti. La regla
única a recordar:

> **¿Cambiaste estado que el framework no conoce** (un contador tuyo, un cálculo, un campo dibujado en el
> `onPaint`)? **Tú** llamas a `repaint()`. (Ver [La paint thread](/docs/concepts/the-paint-thread).)

### El contraejemplo clásico
```java
this.progresso = 0.7f;   // cambié un campo que solo mi onPaint lee…
// …y la pantalla no cambia. Falta:
Component.repaint();      // ✔ ahora el próximo frame refleja el nuevo valor
```
"Cambié el estado y la pantalla no se actualizó" es, casi siempre, un `repaint()` faltante.

## Por qué esta elección
- **Previsibilidad:** el flujo es lineal y visible — dato → setter → repaint → frame. No hay
  recomposición mágica ni ciclo reactivo escondido que depurar.
- **Cero magia, bajo costo:** sin grafo de dependencias reactivo, sin framework de DI que configurar.
- El precio es la explicitud: **gestionar el estado es responsabilidad de la aplicación**, no del framework.

## Los mecanismos de comunicación que ofrece el framework
- **Callbacks / actions** — `onAction(...)`, callbacks pasados por constructor (ver
  [Devolver valores](/docs/ui/navigation/returning-results)).
- **Jerarquía padre/hijo** — navegar el árbol de componentes.
- **`property(k, v)`** — el "saco de datos" por componente (ver [Todo es un Component](/docs/concepts/everything-is-a-component)).
- **Estado global** — campos estáticos/servicios compartidos cuando tenga sentido.

## El patrón recomendado
Como el framework solo da los bloques, el patrón de organización (un **ViewModel/controller** por pantalla +
**repositorio** para los datos) es convención de la aplicación — documentado en
[best-practices](/docs/best-practices). El framework no impone arquitectura; ofrece
componentes y el par setter→`repaint()`.

<div class="cf-diagram">
<svg role="img" id="my-svg" width="986" height="78" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="flowchart" style="max-width: 985.797px; background-color: transparent;" viewBox="0 0 985.796875 77.98837280273438" 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="M193.047,39L198.805,39C204.563,39,216.078,39,226.927,39C237.776,39,247.958,39,253.049,39L258.141,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="W3sieCI6MTkzLjA0Njg3NSwieSI6Mzl9LHsieCI6MjI3LjU5Mzc1LCJ5IjozOX0seyJ4IjoyNjIuMTQwNjI1LCJ5IjozOX1d" data-look="classic" marker-end="url(#my-svg_flowchart-v2-pointEnd)"/><path d="M431.609,28.572L445.25,26.893C458.891,25.215,486.172,21.857,512.793,21.982C539.414,22.108,565.374,25.715,578.355,27.519L591.335,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="W3sieCI6NDMxLjYwOTM3NSwieSI6MjguNTcyMTMyMDcwMTYyMjc2fSx7IngiOjUxMy40NTMxMjUsInkiOjE4LjV9LHsieCI6NTk1LjI5Njg3NSwieSI6MjkuODczMDg2OTAzNTY0MDV9XQ==" data-look="classic" marker-end="url(#my-svg_flowchart-v2-pointEnd)"/><path d="M726.656,39L734.578,39C742.5,39,758.344,39,773.521,39C788.698,39,803.208,39,810.464,39L817.719,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="W3sieCI6NzI2LjY1NjI1LCJ5IjozOX0seyJ4Ijo3NzQuMTg3NSwieSI6Mzl9LHsieCI6ODIxLjcxODc1LCJ5IjozOX1d" data-look="classic" marker-end="url(#my-svg_flowchart-v2-pointEnd)"/><path d="M595.297,48.127L581.656,50.022C568.016,51.918,540.734,55.709,514.115,56.007C487.495,56.305,461.537,53.111,448.558,51.514L435.579,49.916" 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="W3sieCI6NTk1LjI5Njg3NSwieSI6NDguMTI2OTEzMDk2NDM1OTV9LHsieCI6NTEzLjQ1MzEyNSwieSI6NTkuNX0seyJ4Ijo0MzEuNjA5Mzc1LCJ5Ijo0OS40Mjc4Njc5Mjk4Mzc3MjR9XQ==" data-look="classic" marker-end="url(#my-svg_flowchart-v2-pointEnd)"/></g><g class="edgeLabels"><g class="edgeLabel" transform="translate(227.59375, 39)"><g class="label" data-id="L_Model_VM_0" transform="translate(-9.546875, -10.5)"><foreignObject width="19.09375" 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>lee</p></span></div></foreignObject></g></g><g class="edgeLabel" transform="translate(513.453125, 18.5)"><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(774.1875, 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(513.53682, 59.48837)"><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(100.5234375, 39)"><rect class="basic label-container" style="" x="-92.5234375" y="-24.5" width="185.046875" height="49"/><g class="label" style="" transform="translate(-64.5234375, -10.5)"><rect/><foreignObject width="129.046875" 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>modelo / repositorio</p></span></div></foreignObject></g></g><g class="node default" id="my-svg-flowchart-VM-1" data-look="classic" transform="translate(346.875, 39)"><rect class="basic label-container" style="" x="-84.734375" y="-24.5" width="169.46875" height="49"/><g class="label" style="" transform="translate(-56.734375, -10.5)"><rect/><foreignObject width="113.46875" 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 (tuyo)</p></span></div></foreignObject></g></g><g class="node default" id="my-svg-flowchart-Comp-3" data-look="classic" transform="translate(660.9765625, 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(899.7578125, 39)"><rect class="basic label-container" style="" x="-78.0390625" y="-24.5" width="156.078125" height="49"/><g class="label" style="" transform="translate(-50.0390625, -10.5)"><rect/><foreignObject width="100.078125" 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>siguiente 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>

## Ver también
- Relacionado: [La paint thread](/docs/concepts/the-paint-thread) · [Arquitectura de app (best-practices)](/docs/best-practices)
