What is the best way handle String in multiple closures? #5319
Replies: 3 comments
-
|
Also I have something similar to: Here id is an integer, if it is a string it is not even possible to clone to the async onclick unless I am missing something |
Beta Was this translation helpful? Give feedback.
-
|
https://docs.rs/dioxus/0.7.3/dioxus/prelude/macro.to_owned.html |
Beta Was this translation helpful? Give feedback.
-
|
The idiomatic Dioxus way is the #[component]
fn TestComponent() -> Element {
let path_loader: Loader<Vec<String>> = use_loader(test_api)?;
rsx! {
for path in path_loader() {
div {
p { "path: {path}" }
input {
r#type: "checkbox",
oninput: {
to_owned![path];
move |event: Event<FormData>| {
info!("Selected {} {}", event.checked(), path);
}
},
}
button {
onclick: {
to_owned![path];
move |_| {
info!("Deleted {}", path);
}
},
"delete {path}"
}
button {
onclick: {
to_owned![path];
move |_| {
info!("Edit {}", path);
}
},
"edit {path}"
}
}
}
}
}
You can also clone multiple variables at once: @canyon-service regarding the onclick: {
to_owned![url];
move |_| async move {
// url is owned here
do_something(url).await;
}
}See the macro docs for details. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a component that loops through a vector of Strings that is passed to multiple closures, one issue is that it cannot be passed to multiple closures. I have a fix to manually clone the String 2 times for 2 closures. Is there a better way of doing this?
Ugly fix
Beta Was this translation helpful? Give feedback.
All reactions