1
0

basic.rs 2.2 KB

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