basic.rs 2.2 KB

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