mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-10 06:34:20 +00:00
wip: add use_callback hook
This commit is contained in:
parent
6c677e64da
commit
57c10174ec
4 changed files with 62 additions and 6 deletions
20
examples/callback.rs
Normal file
20
examples/callback.rs
Normal file
|
@ -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!"
|
||||
}
|
||||
})
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
use dioxus::prelude::*;
|
||||
use std::collections::HashMap;
|
||||
use std::{collections::HashMap, marker::PhantomData};
|
||||
|
||||
fn main() {
|
||||
dioxus_desktop::launch(|cx| {
|
||||
|
@ -65,7 +65,7 @@ async fn breed_pic(cx: Scope, breed: String) -> Element {
|
|||
});
|
||||
|
||||
match fut.await {
|
||||
Ok(resp) => cx.render(rsx! {
|
||||
Ok(resp) => render! {
|
||||
div {
|
||||
button {
|
||||
onclick: move |_| fut.restart(),
|
||||
|
@ -77,7 +77,7 @@ async fn breed_pic(cx: Scope, breed: String) -> Element {
|
|||
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;
|
||||
pub use useeffect::*;
|
||||
|
||||
// mod usesuspense;
|
||||
// pub use usesuspense::*;
|
||||
mod usecallback;
|
||||
pub use usecallback::*;
|
||||
|
|
36
packages/hooks/src/usecallback.rs
Normal file
36
packages/hooks/src/usecallback.rs
Normal file
|
@ -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 {
|
||||
// //
|
||||
// });
|
||||
}
|
Loading…
Reference in a new issue