dioxus/examples
2022-04-21 06:29:22 -05:00
..
assets feat: default asset server 2022-01-27 16:36:17 -05:00
core_reference Convert all logs to trace 2022-03-27 09:23:44 -06:00
all_css.rs feat: add all the css 2022-02-11 15:50:07 -05:00
borrowed.rs examples: clippy 2022-02-28 02:38:17 -05:00
calculator.rs feat: re-unify set_state 2022-03-04 14:29:45 -05:00
crm.rs docs: even more docs 2022-03-06 20:37:57 -05:00
custom_assets.rs examples: fixup asset example 2022-01-27 16:37:09 -05:00
custom_html.rs feat: allow customizing the index and head 2022-04-16 16:53:47 -04:00
disabled.rs feat: re-unify set_state 2022-03-04 14:29:45 -05:00
dog_app.rs feat: update release posts 2022-03-09 18:11:40 -05:00
error_handle.rs docs: more docs and an errorhandling example 2022-03-08 14:15:18 -05:00
eval.rs add a desktop eval example 2022-03-19 03:11:30 +01:00
events.rs fix: dblclick instead of doubleclick 2022-03-11 10:49:38 -05:00
fermi.rs feat: add a redirect 2022-03-05 14:06:54 -05:00
file_explorer.rs examples: clippy 2022-02-28 02:38:17 -05:00
filedragdrop.rs fix: some examples 2022-03-05 15:25:09 -05:00
flat_router.rs fix: format examples 2022-03-05 16:10:17 -05:00
form.rs Add some radio buttons to the form example 2022-03-19 17:05:48 -06:00
framework_benchmark.rs feat: re-unify set_state 2022-03-04 14:29:45 -05:00
heavy_compute.rs fix: ping window after virtualdom is ready 2022-02-19 17:34:44 -05:00
hello_world.rs chore: remove unused import in example 2022-01-07 22:13:05 -05:00
hydration.rs feat: re-unify set_state 2022-03-04 14:29:45 -05:00
inlineprops.rs docs: an example for inlineprops 2022-03-20 20:59:50 +01:00
inputs.rs fix: some examples 2022-03-05 15:25:09 -05:00
link.rs fix: some examples 2022-03-05 15:25:09 -05:00
login_form.rs fix: login example 2022-02-23 08:44:16 -05:00
nested_listeners.rs fix: some examples 2022-03-05 15:25:09 -05:00
optional_props.rs updated examples with the new optional props 2022-03-16 06:36:39 +00:00
order.rs fix: instantly resolving futures should not use popping 2022-03-24 11:15:07 -04:00
pattern_model.rs chore: make clippy happier 2022-01-24 19:52:12 -05:00
pattern_reducer.rs feat: re-unify set_state 2022-03-04 14:29:45 -05:00
README.md Merge branch 'master' into eval-stuff 2022-03-21 22:00:17 +01:00
readme.rs feat: enable addassign for usestate 2022-03-05 17:07:34 -05:00
router.rs fix: some examples 2022-03-05 15:25:09 -05:00
rsx_compile_fail.rs feat: re-unify set_state 2022-03-04 14:29:45 -05:00
rsx_usage.rs docs: remove all usages of static closure syntax and update readme 2022-01-02 18:35:38 -05:00
ssr.rs feat: overhaul examples and clean things up 2022-01-03 00:42:17 -05:00
suspense.rs feat: use future fully figured out 2022-02-21 15:39:47 -05:00
svg.rs feat: re-unify set_state 2022-03-04 14:29:45 -05:00
tailwind.rs docs: use tailwindv3 in examples 2022-04-16 16:59:41 -04:00
tasks.rs docs: even more docs 2022-03-06 20:37:57 -05:00
textarea.rs feat: re-unify set_state 2022-03-04 14:29:45 -05:00
todomvc.rs fix: some examples 2022-03-05 15:25:09 -05:00
tui_border.rs feat: update examples for tui 2022-03-09 13:36:30 -05:00
tui_color_test.rs add builder pattern and update examples 2022-04-21 06:29:22 -05:00
tui_components.rs feat: update examples for tui 2022-03-09 13:36:30 -05:00
tui_frame.rs feat: update examples for tui 2022-03-09 13:36:30 -05:00
tui_hover.rs feat: update examples for tui 2022-03-09 13:36:30 -05:00
tui_keys.rs feat: update examples for tui 2022-03-09 13:36:30 -05:00
tui_list.rs feat: update examples for tui 2022-03-09 13:36:30 -05:00
tui_margin.rs feat: update examples for tui 2022-03-09 13:36:30 -05:00
tui_quadrants.rs feat: update examples for tui 2022-03-09 13:36:30 -05:00
tui_readme.rs feat: update examples for tui 2022-03-09 13:36:30 -05:00
tui_task.rs feat: update examples for tui 2022-03-09 13:36:30 -05:00
tui_text.rs feat: update examples for tui 2022-03-09 13:36:30 -05:00
window_event.rs feat: re-unify set_state 2022-03-04 14:29:45 -05:00
xss_safety.rs feat: re-unify set_state 2022-03-04 14:29:45 -05:00

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
    }
  })
}