Skip to content

Add Writable::map for Signal Mapping #3610

@cwahn

Description

@cwahn

Feature Request

Description

Currently, Dioxus Signals lacks a Writable::map feature, making it difficult to derive a Signal<T> from a parent Signal<Vec<T>>. This is particularly important in scenarios like dynamic form generation or nested component state management, where child components require fine-grained reactivity while maintaining synchronization with the parent state.

For example, a parent component may receive a Signal<Vec<T>> and need to pass individual Signal<T> to its children. The lack of Writable::map forces developers to pass the Signal<Vec<T>> and the index separately and manually index into the vector on every access.


Implement Suggestion

Introduce a Writable::map function to create a mutable view into a subset of a signalized structure. With this, developers can map a Signal<Vec<T>> to individual Signal<T> for child components in an ergonomic and efficient manner.

Example:

forms_signal.map_mut(move |forms| &mut forms[index])

This approach would simplify the following common use case:

#[component]
fn ParentComponent(props: ParentProps) -> Element {
    let forms_signal = props.forms_signal;

    rsx! {
        div {
            forms_signal.read().iter().enumerate().map(|(index, form)| rsx!(
                ChildComponent {
                    form_signal: ???, // Derive Signal<T> here
                }
            ))
        }
    }
}

With Writable::map, it becomes easy to pass individual signals to children while ensuring modifications in children sync back to the parent signalized vector.


Why This Feature Matters

This feature is crucial for:

  1. Dynamic form generation (e.g., JSON-driven forms).
  2. Nested component state management.
  3. Any pattern requiring signalized collections with fine-grained reactivity.

It improves code ergonomics, reduces verbosity, and minimizes potential errors from manual indexing.


Workaround

Currently, developers can pass the Signal<Vec<T>> and index separately to child components and manually index into the vector. However, this approach is less ergonomic and more verbose compared to having a dedicated Writable::map.


Related Discussions and Issues


You can now submit this issue on the project’s GitHub!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestsignalsRelated to the signals crate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions