Skip to content

State and Lifecycle

PAM Native has a complete application-state model, not just mutable values attached to views. Components stay alive, reads are dependency-tracked, writes schedule retained renders, Rust computes minimal mutations, and Android/iOS commit them on the native frame boundary.

State Best owner Example
Temporary state for one component Local component state expanded card, input draft
Shared domain state Global store session, cart, settings
One subtree or flow provide() / inject() checkout or modal context
High-frequency platform interaction Native component cursor, scroll, animation
Restorable component instance Restorable editor draft, selected tab

This separation prevents a global store from becoming a dumping ground and keeps per-frame interaction away from PHP.

Mark a property with #[State] and assign it from an action. The assignment schedules a render, and the value survives hot reload:

use Pam\Native\Attributes\State;
final class Counter extends Component
{
#[State]
public int $count = 0;
public function increment(): void
{
$this->count++;
}
public function render(): Element
{
return Button::make((string) $this->count)
->onPress($this->increment(...));
}
}

For derived values and reactions, create signals in setup(). Everything created there belongs to the component and is released when it unmounts:

src/Components/SearchBox.php
<?php
declare(strict_types=1);
namespace App\Components;
use Pam\Native\Component;
use Pam\Native\Element;
use Pam\Native\Signals\ComputedSignal;
use Pam\Native\Signals\Signal;
use Pam\Native\UI\Column;
use Pam\Native\UI\Text;
use function Pam\Native\computed;
use function Pam\Native\signal;
use function Pam\Native\watch;
final class SearchBox extends Component
{
public Signal $query;
public ComputedSignal $summary;
public function setup(): void
{
$this->query = signal('');
$this->summary = computed(fn () => $this->query->value === ''
? 'Type to search'
: "Searching for {$this->query->value}");
watch($this->query, fn (string $next, string $previous) => error_log("query: {$previous} -> {$next}"));
}
public function render(): Element
{
return Column::make(Text::make($this->summary->value));
}
}

computed() is cached and recomputes only after a signal it read has changed. watch() calls back with the next and previous value on every change. In templates, read a signal as {{ $query.value }}.

final class AppStore extends Store
{
protected function state(): array
{
return ['theme' => 1, 'online' => true, 'notifications' => 0];
}
public function markAllRead(): void
{
$this->notifications = 0;
}
}
$app = Stores::get(AppStore::class);
$app->dispatch('markAllRead');

Stores add Vuex-style actions, computed state, transactions, selectors, subscriptions, concurrency policies, optimistic rollback, undo/redo, persistence, encryption, migrations, replication, and time-travel DevTools. Read the complete global store guide.

boot() → setup() → mount() → first render → native commit → onMounted() → onFocus()
→ renders when state changes
→ onBlur() → onUnmounted() → setup() scope released

Register hooks inside setup() with the functions from the Pam\Native namespace:

Hook Runs
onMounted(Closure) Once per mount, after the first native commit.
onFocus(Closure) Each time the screen becomes visible.
onBlur(Closure) Each time the screen is covered or left, and at unmount while focused.
onUnmounted(Closure) At unmount, before the scope’s subscriptions are released.

Use focus hooks to start and stop work that only matters while a screen is visible, such as polling, sensors or animations. Watchers, composables, resources and component timers created in setup() are released automatically at unmount; component timers are also suspended while the app is in the background. The component runtime reference covers every hook, typed props, computed values, memo, watchers, refs, slots, events, context, restoration and error boundaries, including the older attached() and cleanup() methods.

PHP component/store write
→ dependency invalidation
→ retained Element tree
→ Rust reconcile + layout
→ bounded PNB1 mutation batch
→ one Kotlin/Swift UI commit per frame

The first frame sends a complete tree. Later frames contain numeric creates, removes, property updates, moves, and root changes. Stable 64-bit keyed identities let PAM retain unchanged native nodes.

The platform keeps high-frequency behavior local:

  • input text, cursor, selection, and IME;
  • press, long-press, pan, pinch, and rotation recognition;
  • scroll, fling, recycled-list prefetch, and refresh;
  • image fetch, decode, disk cache, and progress;
  • video/audio playback and media cache;
  • modal, drawer, navigation, and transform animations; and
  • safe-area, keyboard, dimensions, and accessibility behavior.

PHP receives semantic events. Coalesced progress/pointer events are delivered at most once per frame rather than turning every native motion sample into an application render.

Mode PHP synchronization
Native No per-keystroke synchronization
Debounced Deliver after a short quiet period
Immediate Deliver every accepted change
Blur Deliver when focus leaves
Submit Deliver on submission

Text and selection stay native while the user edits, so a slow domain action cannot make the field lose its cursor.

  • Give moving/repeated children stable keys.
  • Keep render() pure and bounded.
  • Read only the store/local-state values the component actually needs.
  • Put network and disk work in services or actions.
  • Use native lists for large data sets and native media/cache components for images, video, and files.
  • Use computed values and selectors for derived data.
  • Release manual subscriptions and external handles through effect cleanup.