Part 4 of 10 18 Jan 2026 By Raj Patil 25 min read

Part 4: State Management in Freya - Signals & Hooks

Part 4 of the Freya Rust GUI series. Master state management in Rust GUIs using `use_state`, signals, and reactive data flow patterns.

Intermediate #rust #freya #state-management #hooks #signals #gui #rust-ui
Building Native GUIs with Rust & Freya 4 / 10

State Management in Freya

State management is crucial for interactive applications. I’ll show you how Freya handles state.

Using use_state

The use_state hook allows you to maintain state across renders:

use freya::prelude::*;

fn app() -> impl IntoElement {
    let mut count = use_state(|| 0);

    rect()
        .width(Size::fill())
        .height(Size::fill())
        .direction(Direction::Vertical)
        .cross_align(Alignment::Center)
        .child(
            label()
                .font_size(24.0)
                .child(format!("Count: {}", count.read()))
        )
        .child(
            Button::new()
                .on_press(move |_| *count.write() += 1)
                .child(label().text("Increment"))
        )
}

State Patterns

Toggle State

fn app() -> impl IntoElement {
    let mut is_toggled = use_state(|| false);

    Button::new()
        .on_press(move |_| {
            let current = *is_toggled.read();
            is_toggled.set(!current);
        })
        .child(
            label().text(if *is_toggled.read() { "ON" } else { "OFF" })
        )
}

List State

fn app() -> impl IntoElement {
    let mut items = use_state(|| vec!["Item 1".to_string()]);

    rect()
        .direction(Direction::Vertical)
        // Render items
        .children(
            items.read().iter().map(|item| {
                label().text(item.clone())
            })
        )
        .child(
            Button::new()
                .on_press(move |_| {
                    items.write().push(format!("Item {}", items.read().len() + 1));
                })
                .child(label().text("Add Item"))
        )
}

What’s Next

Next, I’ll learn about event handling in depth.

Summary