wip: recoil
This commit is contained in:
1 parent
3cfa1fe125
commit
ee67654f58
31 files changed
+1369
-235
No files matched your search
@@ -0,0 +1,36 @@
|
||||
//! Example: RecoilCallback
|
||||
//! ------------------------
|
||||
//! This example shows how use_recoil_callback can be used to abstract over sets/gets.
|
||||
//! This hook provides a way to capture the RecoilApi object. In this case, we capture
|
||||
//! it in a closure an abstract the set/get functionality behind the update_title function.
|
||||
//!
|
||||
//! It should be straightforward to build a complex app with recoil_callback.
|
||||
use dioxus_core::prelude::*;
|
||||
use recoil::*;
|
||||
|
||||
const TITLE: Atom<&str> = |_| "red";
|
||||
|
||||
fn update_title(api: &RecoilApi) {
|
||||
match *api.get(&TITLE) {
|
||||
"green" => api.set(&TITLE, "yellow"),
|
||||
"yellow" => api.set(&TITLE, "red"),
|
||||
"red" => api.set(&TITLE, "green"),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
static App: FC<()> = |ctx, _| {
|
||||
let title = use_recoil_value(ctx, &TITLE);
|
||||
let next_light = use_recoil_callback(ctx, |api| move |_| update_title(&api));
|
||||
|
||||
rsx! { in ctx,
|
||||
div {
|
||||
"{title}"
|
||||
button { onclick: {next_light}, "Next light" }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fn main() {
|
||||
wasm_bindgen_futures::spawn_local(dioxus_web::WebsysRenderer::start(App))
|
||||
}
|
||||
Reference in new issue
Block a user