Jonathan Kelley 07529e491c fix: subscription should apply internally to hook 3 years ago
..
assets 430cde7068 feat: default asset server 3 years ago
core_reference 213f54c4d8 Convert all logs to trace 3 years ago
README.md ee22881e08 Merge branch 'master' into eval-stuff 3 years ago
all_css.rs 9f60dfa8b0 feat: add all the css 3 years ago
borrowed.rs 901bd5b0cd examples: clippy 3 years ago
calculator.rs 6c3f5195f4 feat: re-unify set_state 3 years ago
crm.rs 0f87ebb277 docs: even more docs 3 years ago
custom_assets.rs 6e4ed9e351 examples: fixup asset example 3 years ago
custom_element.rs f67b71c5a5 feat: custom element example 3 years ago
custom_html.rs 049976d23a feat: allow customizing the index and head 3 years ago
disabled.rs 6c3f5195f4 feat: re-unify set_state 3 years ago
dog_app.rs ff81f1c819 feat: update release posts 3 years ago
error_handle.rs 3acd643afb docs: more docs and an errorhandling example 3 years ago
eval.rs 5e475fadd9 add a desktop eval example 3 years ago
events.rs 841ad91fe1 fix: dblclick instead of doubleclick 3 years ago
fermi.rs 07529e491c fix: subscription should apply internally to hook 3 years ago
file_explorer.rs 901bd5b0cd examples: clippy 3 years ago
filedragdrop.rs 35b22923b2 fix: some examples 3 years ago
flat_router.rs fb1a848124 fix: format examples 3 years ago
form.rs ae2e8a8038 Add some radio buttons to the form example 3 years ago
framework_benchmark.rs 6c3f5195f4 feat: re-unify set_state 3 years ago
heavy_compute.rs 28716248c5 fix: ping window after virtualdom is ready 3 years ago
hello_world.rs 43a368c8bd chore: remove unused import in example 3 years ago
hydration.rs 6c3f5195f4 feat: re-unify set_state 3 years ago
inlineprops.rs ff459f7297 docs: an example for inlineprops 3 years ago
inputs.rs 35b22923b2 fix: some examples 3 years ago
link.rs 35b22923b2 fix: some examples 3 years ago
login_form.rs 07bea88fcc fix: login example 3 years ago
nested_listeners.rs 35b22923b2 fix: some examples 3 years ago
optional_props.rs a2825fb13c updated examples with the new optional props 3 years ago
order.rs d50e86492e fix: instantly resolving futures should not use popping 3 years ago
pattern_model.rs 26ada2c7c1 chore: make clippy happier 3 years ago
pattern_reducer.rs 6c3f5195f4 feat: re-unify set_state 3 years ago
readme.rs a4ab2d9de0 feat: enable addassign for usestate 3 years ago
router.rs 35b22923b2 fix: some examples 3 years ago
rsx_compile_fail.rs 6c3f5195f4 feat: re-unify set_state 3 years ago
rsx_usage.rs cafb7df736 docs: remove all usages of static closure syntax and update readme 3 years ago
ssr.rs 420a30e5d4 feat: overhaul examples and clean things up 3 years ago
suspense.rs 9211e1fc78 feat: use future fully figured out 3 years ago
svg.rs 6c3f5195f4 feat: re-unify set_state 3 years ago
tailwind.rs 29bda8aa48 docs: use tailwindv3 in examples 3 years ago
tasks.rs 0f87ebb277 docs: even more docs 3 years ago
textarea.rs 6c3f5195f4 feat: re-unify set_state 3 years ago
todomvc.rs 35b22923b2 fix: some examples 3 years ago
tui_border.rs 50d509688d feat: update examples for tui 3 years ago
tui_color_test.rs 9d9aa33e25 add builder pattern and update examples 3 years ago
tui_components.rs 50d509688d feat: update examples for tui 3 years ago
tui_frame.rs 50d509688d feat: update examples for tui 3 years ago
tui_hover.rs 50d509688d feat: update examples for tui 3 years ago
tui_keys.rs 50d509688d feat: update examples for tui 3 years ago
tui_list.rs 50d509688d feat: update examples for tui 3 years ago
tui_margin.rs 50d509688d feat: update examples for tui 3 years ago
tui_quadrants.rs 50d509688d feat: update examples for tui 3 years ago
tui_readme.rs 50d509688d feat: update examples for tui 3 years ago
tui_task.rs 50d509688d feat: update examples for tui 3 years ago
tui_text.rs 50d509688d feat: update examples for tui 3 years ago
window_event.rs 6c3f5195f4 feat: re-unify set_state 3 years ago
xss_safety.rs 6c3f5195f4 feat: re-unify set_state 3 years ago

README.md

Examples

Most of these examples are run through webview so you don't need the Dioxus CLI installed to preview the functionality.

These examples are fully-fledged micro apps. They can be ran with the cargo run --example XYZ

Example What it does Status
The basics A few basic examples to preview Dioxus 🛠
fine grained reactivity Escape diffing by writing values directly 🛠
Global State Management Share state between components 🛠
[Virtual Refs]() Cross-platform imperative elements 🛠
Inline Styles Define styles for elements inline 🛠
Conditional Rendering Hide/Show elements using conditionals

These examples are not necessarily meant to be run, but rather serve as a reference for the given functionality.

Example What it does Status
The basics A few basic examples to preview Dioxus 🛠
fine grained reactivity Escape diffing by writing values directly 🛠
Global State Management Share state between components 🛠
[Virtual Refs]() Cross-platform imperative elements 🛠
Inline Styles Define styles for elements inline 🛠
Conditional Rendering Hide/Show elements using conditionals
Maps/Iterators Use iterators in the rsx! macro
Render To string Render a mounted virtualdom to a string 🛠
Component Children Pass children into child components 🛠
[Function Driven children]() Pass functions to make VNodes 🛠
Memoization & Borrowed Data Suppress renders, borrow from parents
Fragments Support root-less element groups
Null/None Components Return nothing! 🛠
Spread Pattern for props Manually specify and override props
Controlled Inputs this does 🛠
[Custom Elements]() Define custom elements 🛠
[Web Components]() Custom elements to interface with WebComponents 🛠
[Testing And debugging]() this does 🛠
[Asynchronous Data]() Using suspense to wait for data 🛠
[Fiber/Scheduled Rendering]() this does 🛠
[CSS Compiled Styles]() this does 🛠
Anti-patterns A collection of discouraged patterns
Complete rsx reference A complete reference for all rsx! usage
Event Listeners Attach closures to events on elements
Inline Props Using the #[inline_props] macro
Eval Evaluate dynamic JavaScript code

Show me some examples!

In our collection of examples, guides, and tutorials, we have:

  • The book (an introductory course)
  • The guide (an in-depth analysis of everything in Dioxus)
  • The reference (a collection of examples with heavy documentation)
  • The general examples
  • The platform-specific examples (web, ssr, desktop, mobile, server)

Here's what a few common tasks look like in Dioxus:

Nested components with children and internal state:

fn App(cx: Scope) -> Element {
  cx.render(rsx!( Toggle { "Toggle me" } ))
}

#[derive(PartialEq, Props)]
struct ToggleProps { children: Element }

fn Toggle(cx: Scope<ToggleProps>) -> Element {
  let mut toggled = use_state(&cx, || false);
  cx.render(rsx!{
    div {
      &cx.props.children
      button { onclick: move |_| toggled.set(true),
        toggled.and_then(|| "On").or_else(|| "Off")
      }
    }
  })
}

Controlled inputs:

fn App(cx: Scope) -> Element {
  let value = use_state(&cx, String::new);
  cx.render(rsx!(
    input {
      "type": "text",
      value: "{value}",
      oninput: move |evt| value.set(evt.value.clone())
    }
  ))
}

Lists and Conditional rendering:

fn App(cx: Scope) -> Element {
  let list = (0..10).map(|i| {
    rsx!(li { key: "{i}", "Value: {i}" })
  });

  let title = match list.len() {
    0 => rsx!("Not enough"),
    _ => rsx!("Plenty!"),
  };

  if should_show {
    cx.render(rsx!(
      title,
      ul { list }
    ))
  } else {
    None
  }
}

Tiny components:

static App: Component = |cx| rsx!(cx, div {"hello world!"});

Borrowed prop contents:

fn App(cx: Scope) -> Element {
  let name = use_state(&cx, || String::from("example"));
  rsx!(cx, Child { title: name.as_str() })
}

#[derive(Props)]
struct ChildProps<'a> { title: &'a str }

fn Child(cx: Scope<ChildProps>) -> Element {
  rsx!(cx, "Hello {cx.props.title}")
}

Global State

struct GlobalState { name: String }

fn App(cx: Scope) -> Element {
  use_provide_shared_state(cx, || GlobalState { name: String::from("Toby") })
  rsx!(cx, Leaf {})
}

fn Leaf(cx: Scope) -> Element {
  let state = use_consume_shared_state::<GlobalState>(cx)?;
  rsx!(cx, "Hello {state.name}")
}

Router (inspired by Yew-Router)

#[derive(PartialEq, Clone,  Hash, Eq, Routable)]
enum Route {
  #[at("/")]
  Home,
  #[at("/post/{id}")]
  Post(id)
}

fn App(cx: Scope) -> Element {
  let route = use_router(cx, Route::parse);
  cx.render(rsx!(div {
    match route {
      Route::Home => rsx!( Home {} ),
      Route::Post(id) => rsx!( Post { id: id })
    }
  }))
}

Suspense

fn App(cx: Scope) -> Element {
  let doggo = use_suspense(cx,
    || async { reqwest::get("https://dog.ceo/api/breeds/image/random").await.unwrap().json::<Response>().await.unwrap() },
    |response| cx.render(rsx!( img { src: "{response.message}" }))
  );

  cx.render(rsx!{
    div {
      "One doggo coming right up:",
      doggo
    }
  })
}