Change context api to panic with nice error message
This commit is contained in:
19 files changed
+96
-69
No files matched your search
+7
-5
@@ -1,8 +1,9 @@
|
||||
//! This example shows how to create a popup window and send data back to the parent window.
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use futures_util::StreamExt;
|
||||
use tokio::sync::mpsc::UnboundedSender;
|
||||
|
||||
fn main() {
|
||||
dioxus_desktop::launch(app);
|
||||
@@ -12,15 +13,16 @@ fn app() -> Element {
|
||||
let emails_sent = use_signal(|| Vec::new() as Vec<String>);
|
||||
|
||||
// Wait for responses to the compose channel, and then push them to the emails_sent signal.
|
||||
let tx = use_coroutine(|mut rx: UnboundedReceiver<String>| async move {
|
||||
let handle = use_coroutine(|mut rx: UnboundedReceiver<String>| async move {
|
||||
while let Some(message) = rx.next().await {
|
||||
emails_sent.write().push(message);
|
||||
}
|
||||
});
|
||||
|
||||
let open_compose_window = move |evt: MouseEvent| {
|
||||
let tx = handle.tx();
|
||||
dioxus_desktop::window().new_window(
|
||||
VirtualDom::new_with_props(compose, tx.clone()),
|
||||
VirtualDom::new_with_props(compose, Rc::new(move |s| tx.unbounded_send(s))),
|
||||
Default::default(),
|
||||
);
|
||||
};
|
||||
@@ -39,7 +41,7 @@ fn app() -> Element {
|
||||
}
|
||||
}
|
||||
|
||||
fn compose(tx: UnboundedSender<String>) -> Element {
|
||||
fn compose(send: Rc<dyn Fn(String)>) -> Element {
|
||||
let user_input = use_signal(String::new);
|
||||
|
||||
rsx! {
|
||||
@@ -48,7 +50,7 @@ fn compose(tx: UnboundedSender<String>) -> Element {
|
||||
|
||||
button {
|
||||
onclick: move |_| {
|
||||
tx.send(user_input.get().clone());
|
||||
send(user_input.get().clone());
|
||||
dioxus_desktop::window().close();
|
||||
},
|
||||
"Click to send"
|
||||
|
||||
+40
-35
@@ -3,29 +3,17 @@ use dioxus::prelude::*;
|
||||
use dioxus_router::prelude::*;
|
||||
|
||||
fn main() {
|
||||
dioxus_desktop::launch(App);
|
||||
dioxus_desktop::launch(app);
|
||||
}
|
||||
|
||||
#[derive(Routable, Clone)]
|
||||
#[rustfmt::skip]
|
||||
enum Route {
|
||||
#[route("/")]
|
||||
ClientList {},
|
||||
#[route("/new")]
|
||||
ClientAdd {},
|
||||
#[route("/settings")]
|
||||
Settings {},
|
||||
}
|
||||
/// A type alias that reprsents a shared context between components
|
||||
///
|
||||
/// Normally we'd wrap the Context in a newtype, but we only have one Signal<Vec<Client>> in this app
|
||||
type Clients = Signal<Vec<Client>>;
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Client {
|
||||
pub first_name: String,
|
||||
pub last_name: String,
|
||||
pub description: String,
|
||||
}
|
||||
fn app() -> Element {
|
||||
use_context_provider::<Clients>(|| Signal::new(vec![]));
|
||||
|
||||
#[component]
|
||||
fn App() -> Element {
|
||||
render! {
|
||||
link {
|
||||
rel: "stylesheet",
|
||||
@@ -48,8 +36,30 @@ fn App() -> Element {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Routable, Clone)]
|
||||
#[rustfmt::skip]
|
||||
enum Route {
|
||||
#[route("/")]
|
||||
ClientList {},
|
||||
|
||||
#[route("/new")]
|
||||
ClientAdd {},
|
||||
|
||||
#[route("/settings")]
|
||||
Settings {},
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Client {
|
||||
pub first_name: String,
|
||||
pub last_name: String,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn ClientList() -> Element {
|
||||
let clients = use_context::<Clients>();
|
||||
|
||||
rsx! {
|
||||
h2 { "List of Clients" }
|
||||
Link { to: Route::ClientAdd {}, class: "pure-button pure-button-primary", "Add Client" }
|
||||
@@ -69,21 +79,18 @@ fn ClientAdd() -> Element {
|
||||
let last_name = use_signal(String::new);
|
||||
let description = use_signal(String::new);
|
||||
|
||||
let submit_client = move |_: FormEvent| {
|
||||
consume_context::<Clients>().write().push(Client {
|
||||
first_name: first_name.to_string(),
|
||||
last_name: last_name.to_string(),
|
||||
description: description.to_string(),
|
||||
});
|
||||
dioxus_router::router().push(Route::ClientList {});
|
||||
};
|
||||
|
||||
rsx! {
|
||||
h2 { "Add new Client" }
|
||||
|
||||
form {
|
||||
class: "pure-form pure-form-aligned",
|
||||
onsubmit: move |_| {
|
||||
let mut clients = clients.write();
|
||||
clients
|
||||
.push(Client {
|
||||
first_name: first_name.to_string(),
|
||||
last_name: last_name.to_string(),
|
||||
description: description.to_string(),
|
||||
});
|
||||
dioxus_router::router().push(Route::ClientList {});
|
||||
},
|
||||
form { class: "pure-form pure-form-aligned", onsubmit: submit_client,
|
||||
|
||||
fieldset {
|
||||
div { class: "pure-control-group",
|
||||
@@ -131,14 +138,12 @@ fn ClientAdd() -> Element {
|
||||
|
||||
#[component]
|
||||
fn Settings() -> Element {
|
||||
let clients = use_shared_state::<ClientContext>().unwrap();
|
||||
|
||||
rsx! {
|
||||
h2 { "Settings" }
|
||||
|
||||
button {
|
||||
class: "pure-button pure-button-primary red",
|
||||
onclick: move |_| clients.write().clear(),
|
||||
onclick: move |_| consume_context::<Clients>().write().clear(),
|
||||
"Remove all Clients"
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use dioxus::{core::CapturedError, prelude::*};
|
||||
use dioxus::{dioxus_core::CapturedError, prelude::*};
|
||||
|
||||
fn main() {
|
||||
dioxus_desktop::launch(app);
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
use dioxus::{core::NoOpMutations, prelude::*};
|
||||
use dioxus::{dioxus_core::NoOpMutations, prelude::*};
|
||||
|
||||
fn main() {
|
||||
let mut dom = VirtualDom::new(app);
|
||||
|
||||
Reference in new issue
Block a user