update component on submitting

This commit is contained in:
Evan Almloff 2022-05-28 12:52:49 -05:00
parent 195dd22206
commit eb270ef062

View file

@ -1,23 +1,13 @@
use dioxus::prelude::*; use dioxus::prelude::*;
use std::time::Duration;
fn main() { fn main() {
dioxus::desktop::launch_with_props(with_hot_reload, app, |b| b); dioxus::web::launch_with_props(with_hot_reload, app, |c| c);
} }
fn app(cx: Scope) -> Element { fn app(cx: Scope) -> Element {
let count = use_state(&cx, || 170); let count = use_state(&cx, || 170);
let rsx_code = use_state(&cx, || None); let rsx_code = use_state(&cx, || None);
let re_render = cx.schedule_update();
use_future(&cx, (), move |_| {
let mut count = count.clone();
async move {
loop {
tokio::time::sleep(Duration::from_millis(1000)).await;
count += 1;
}
}
});
cx.render(rsx! { cx.render(rsx! {
div { div {
@ -54,6 +44,7 @@ fn app(cx: Scope) -> Element {
if let Some(code) = rsx_code.get(){ if let Some(code) = rsx_code.get(){
let rsx_text_index: RsxTextIndex = cx.consume_context().unwrap(); let rsx_text_index: RsxTextIndex = cx.consume_context().unwrap();
rsx_text_index.insert(__line_num.clone(), code.clone()); rsx_text_index.insert(__line_num.clone(), code.clone());
re_render();
} }
}, },
"submit" "submit"
@ -65,7 +56,7 @@ fn app(cx: Scope) -> Element {
} }
div { div {
width: format!("{}px", count), width: "{count}px",
height: "10px", height: "10px",
background_color: "red", background_color: "red",
} }
@ -95,7 +86,7 @@ struct CompProps {
fn Comp(cx: Scope<CompProps>) -> Element { fn Comp(cx: Scope<CompProps>) -> Element {
cx.render(rsx! { cx.render(rsx! {
h1 { h1 {
color: cx.props.color, color: "{cx.props.color}",
"Hello, from a component!" "Hello, from a component!"
} }
}) })