wip: add use_callback hook
This commit is contained in:
1 parent
6c677e64da
commit
57c10174ec
4 files changed
+62
-6
No files matched your search
@@ -0,0 +1,20 @@
|
|||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
dioxus_desktop::launch(app);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn app(cx: Scope) -> Element {
|
||||||
|
let login = use_callback!(cx, || |evt| async {
|
||||||
|
//
|
||||||
|
});
|
||||||
|
|
||||||
|
cx.render(rsx! {
|
||||||
|
button {
|
||||||
|
onclick: login,
|
||||||
|
"Click me!"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
+4
-4
@@ -1,5 +1,5 @@
|
|||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
use std::collections::HashMap;
|
use std::{collections::HashMap, marker::PhantomData};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
dioxus_desktop::launch(|cx| {
|
dioxus_desktop::launch(|cx| {
|
||||||
@@ -65,7 +65,7 @@ async fn breed_pic(cx: Scope, breed: String) -> Element {
|
|||||||
});
|
});
|
||||||
|
|
||||||
match fut.await {
|
match fut.await {
|
||||||
Ok(resp) => cx.render(rsx! {
|
Ok(resp) => render! {
|
||||||
div {
|
div {
|
||||||
button {
|
button {
|
||||||
onclick: move |_| fut.restart(),
|
onclick: move |_| fut.restart(),
|
||||||
@@ -77,7 +77,7 @@ async fn breed_pic(cx: Scope, breed: String) -> Element {
|
|||||||
max_height: "500px",
|
max_height: "500px",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
},
|
||||||
Err(_) => cx.render(rsx! { div { "loading dogs failed" } }),
|
Err(_) => render! { div { "loading dogs failed" } },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,5 +19,5 @@ pub use usefuture::*;
|
|||||||
mod useeffect;
|
mod useeffect;
|
||||||
pub use useeffect::*;
|
pub use useeffect::*;
|
||||||
|
|
||||||
// mod usesuspense;
|
mod usecallback;
|
||||||
// pub use usesuspense::*;
|
pub use usecallback::*;
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use dioxus_core::{prelude::EventHandler, ScopeState};
|
||||||
|
use std::future::Future;
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! use_callback {
|
||||||
|
($cx:ident, || || $($rest:tt)*) => { use_callback( $cx, (), |_| $($rest)* ) };
|
||||||
|
($cx:ident, || |$myarg:ident| $($rest:tt)*) => {
|
||||||
|
use_callback(
|
||||||
|
$cx,
|
||||||
|
|| |$myarg| async {}
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
pub fn use_callback<'a, T, R, F>(cx: &'a ScopeState, make: impl FnOnce() -> R) -> impl FnMut(T) + 'a
|
||||||
|
where
|
||||||
|
R: FnMut(T) -> F + 'static,
|
||||||
|
F: Future<Output = ()> + 'static,
|
||||||
|
{
|
||||||
|
let mut hook = make();
|
||||||
|
|
||||||
|
move |evt| cx.spawn(hook(evt))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn it_works(cx: &ScopeState) {
|
||||||
|
let p = use_callback(cx, || {
|
||||||
|
|()| async {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// let p = use_callback!(cx, || |evt| async {
|
||||||
|
// //
|
||||||
|
// });
|
||||||
|
}
|
||||||
Reference in new issue
Block a user