basic.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //! Basic example that renders a simple VNode to the browser.
  2. // all these imports are done automatically with the `dioxus` crate and `prelude`
  3. // need to do them manually for this example
  4. use dioxus::events::on::MouseEvent;
  5. use dioxus_core as dioxus;
  6. use dioxus_core::prelude::*;
  7. use dioxus_hooks::*;
  8. use dioxus_html as dioxus_elements;
  9. use dioxus::prelude::*;
  10. use dioxus_web::*;
  11. use std::future::Future;
  12. use std::{pin::Pin, time::Duration};
  13. fn main() {
  14. // Setup logging
  15. wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
  16. console_error_panic_hook::set_once();
  17. // Run the app
  18. dioxus_web::launch(APP, |c| c)
  19. }
  20. static APP: FC<()> = |cx, props| {
  21. let mut count = use_state(cx, || 3);
  22. let mut content = use_state(cx, || String::from("h1"));
  23. let mut text_content = use_state(cx, || String::from("Hello, world!"));
  24. log::debug!("running scope...");
  25. cx.render(rsx! {
  26. div {
  27. h1 { "content val is {content}" }
  28. input {
  29. r#type: "text",
  30. value: "{text_content}"
  31. oninput: move |e| text_content.set(e.value())
  32. }
  33. br {}
  34. {(0..10).map(|f| {
  35. rsx!(
  36. button {
  37. onclick: move |_| count += 1,
  38. "Click to add."
  39. "Current count: {count}"
  40. }
  41. br {}
  42. )
  43. })}
  44. select {
  45. name: "cars"
  46. id: "cars"
  47. value: "{content}"
  48. oninput: move |ev| {
  49. content.set(ev.value());
  50. match ev.value().as_str() {
  51. "h1" => count.set(0),
  52. "h2" => count.set(5),
  53. "h3" => count.set(10),
  54. _ => {}
  55. }
  56. },
  57. option { value: "h1", "h1" }
  58. option { value: "h2", "h2" }
  59. option { value: "h3", "h3" }
  60. }
  61. {render_list(cx, *count)}
  62. {render_bullets(cx)}
  63. Child {}
  64. }
  65. })
  66. };
  67. fn render_bullets(cx: Context) -> DomTree {
  68. rsx!(cx, div {
  69. "bite me"
  70. })
  71. }
  72. fn render_list(cx: Context, count: usize) -> DomTree {
  73. let items = (0..count).map(|f| {
  74. rsx! {
  75. li { "a - {f}" }
  76. li { "b - {f}" }
  77. li { "c - {f}" }
  78. }
  79. });
  80. rsx!(cx, ul { {items} })
  81. }
  82. static Child: FC<()> = |cx, props| {
  83. // render
  84. rsx!(cx, div {"hello child"})
  85. };