Created
December 17, 2025 20:07
-
-
Save buxx/4c73ac446dd7734115ff5fdfff3b1af5 to your computer and use it in GitHub Desktop.
Leptos example : Struct (not working)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use leptos::prelude::*; | |
| fn main() { | |
| mount_to_body(|| view! { <App /> }); | |
| } | |
| #[derive(Debug, Clone)] | |
| struct Something { | |
| // Will us it as identifier | |
| name: String, | |
| attr: SomeAttr, | |
| } | |
| #[derive(Debug, Clone)] | |
| struct SomeAttr(usize); | |
| #[component] | |
| fn App() -> impl IntoView { | |
| let (things, set_things) = signal(vec![ | |
| ( | |
| "Foo1".to_string(), | |
| ArcRwSignal::new(Something { | |
| name: "Foo1".to_string(), | |
| attr: SomeAttr(42), | |
| }), | |
| ), | |
| ( | |
| "Foo2".to_string(), | |
| ArcRwSignal::new(Something { | |
| name: "Foo1".to_string(), | |
| attr: SomeAttr(24), | |
| }), | |
| ), | |
| ]); | |
| view! { | |
| <button on:click=move |_| simulate_network(set_things)>trigger</button> | |
| <Things things /> | |
| } | |
| } | |
| #[component] | |
| fn Things(things: ReadSignal<Vec<(String, ArcRwSignal<Something>)>>) -> impl IntoView { | |
| view! { | |
| <ul> | |
| <For | |
| each=move || things.get() | |
| key=|thing| thing.0.clone() | |
| children=move |(_, thing)| { | |
| let thing = RwSignal::from(thing); | |
| view! { | |
| <li> | |
| <Thing thing /> | |
| </li> | |
| } | |
| } | |
| /> | |
| </ul> | |
| } | |
| } | |
| #[component] | |
| fn Thing(thing: RwSignal<Something>) -> impl IntoView { | |
| let name = move || thing.get().name; | |
| let attr = thing.get().attr; | |
| view! { | |
| <span>{name}</span> | |
| " " | |
| <Attr attr/> | |
| } | |
| } | |
| #[component] | |
| fn Attr(attr: SomeAttr) -> impl IntoView { | |
| let value = attr.0; | |
| view! { {value} } | |
| } | |
| fn simulate_network(things: WriteSignal<Vec<(String, ArcRwSignal<Something>)>>) { | |
| things.update(|things| { | |
| if let Some((_, thing)) = things.iter().find(|(thing_name, _)| thing_name == "Foo2") { | |
| thing.update(|thing| thing.attr = SomeAttr(128)); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment