Skip to content

Commit d9e6d09

Browse files
committed
docs: examples
1 parent 818043e commit d9e6d09

33 files changed

+732
-152
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ split-debuginfo = "unpacked"
3030

3131
[dev-dependencies]
3232
futures = "0.3.15"
33+
log = "0.4.14"
3334
# For the tide ssr examples
3435
# async-std = { version="1.9.0", features=["attributes"] }
3536
# tide = { version="0.16.0" }

examples/README.md

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,50 @@
11
# Examples
22

3-
Most of these examples are run through webview so you don't need the dioxus cli installed to preview the functionality. Anything labeled `_web` will need to be built with the Dioxus CLI to preview features that only a native bundle can handle.
3+
Most of these examples are run through webview so you don't need the dioxus cli installed to preview the functionality.
44

5-
List of examples:
5+
These examples are fully-fledged micro apps. They can be ran with the `cargo run --example XYZ`
66

7-
| Example | What it does |
8-
| --------------------------------------- | ------------ |
9-
| [The basics](./basics.rs) | this does |
10-
| [fine grained reactivity](./signals.rs) | this does |
11-
| Global State Management | this does |
12-
| Virtual Refs | this does |
13-
| Inline Styles | this does |
14-
| Conditional Rendering | this does |
15-
| Maps/Iterators | this does |
16-
| Render To string | this does |
17-
| Component Children | this does |
18-
| Function Driven children | this does |
19-
| Memoization | this does |
20-
| Borrowed Data | this does |
21-
| Fragments | this does |
22-
| Null/None Components | this does |
23-
| Spread Pattern for props | this does |
24-
| Controlled Inputs | this does |
25-
| Custom Elements | this does |
26-
| Testing And debugging | this does |
27-
| Asynchronous Data | this does |
28-
| Fiber/Scheduled Rendering | this does |
29-
| CSS Compiled Styles | this does |
7+
| Example | What it does | Status |
8+
| --------------------------------------------------- | ------------------------------------------- | ------ |
9+
| [The basics](./basics.rs) | A few basic examples to preview Dioxus | 🛠 |
10+
| [fine grained reactivity](./signals.rs) | Escape `diffing` by writing values directly | 🛠 |
11+
| [Global State Management](./statemanagement.rs) | Share state between components | 🛠 |
12+
| [Virtual Refs]() | Cross-platform imperative elements | 🛠 |
13+
| [Inline Styles](./inline-styles.rs) | Define styles for elements inline | 🛠 |
14+
| [Conditional Rendering](./conditional-rendering.rs) | Hide/Show elements using conditionals ||
15+
16+
These examples are not necessarily meant to be run, but rather serve as a reference for the given functionality.
17+
18+
| Example | What it does | Status |
19+
| --------------------------------------------------- | ----------------------------------------------- | ------ |
20+
| [The basics](./basics.rs) | A few basic examples to preview Dioxus | 🛠 |
21+
| [fine grained reactivity](./signals.rs) | Escape `diffing` by writing values directly | 🛠 |
22+
| [Global State Management](./statemanagement.rs) | Share state between components | 🛠 |
23+
| [Virtual Refs]() | Cross-platform imperative elements | 🛠 |
24+
| [Inline Styles](./inline-styles.rs) | Define styles for elements inline | 🛠 |
25+
| [Conditional Rendering](./conditional-rendering.rs) | Hide/Show elements using conditionals ||
26+
| [Maps/Iterators](./iterators.rs) | Use iterators in the rsx! macro | 🛠 |
27+
| [Render To string](./tostring.rs) | Render a mounted virtualdom to a string | 🛠 |
28+
| [Component Children](./children.rs) | Pass children into child components | 🛠 |
29+
| [Function Driven children]() | Pass functions to make VNodes | 🛠 |
30+
| [Memoization & Borrowed Data](./memo.rs) | Suppress renders, borrow from parents ||
31+
| [Fragments](./fragments.rs) | Support root-less element groups ||
32+
| [Null/None Components](./empty.rs) | Return nothing! | 🛠 |
33+
| [Spread Pattern for props](./spreadpattern.rs) | Manually specify and override props ||
34+
| [Controlled Inputs](./controlled-inputs.rs) | this does | 🛠 |
35+
| [Custom Elements]() | Define custom elements | 🛠 |
36+
| [Web Components]() | Custom elements to interface with WebComponents | 🛠 |
37+
| [Testing And debugging]() | this does | 🛠 |
38+
| [Asynchronous Data]() | Using suspense to wait for data | 🛠 |
39+
| [Fiber/Scheduled Rendering]() | this does | 🛠 |
40+
| [CSS Compiled Styles]() | this does | 🛠 |
41+
| [Anti-patterns](./antipatterns.rs) | A collection of discouraged patterns ||
42+
| [Complete rsx reference](./rsx_usage.rs) | A complete reference for all rsx! usage ||
43+
| [Event Listeners](./listener.rs) | Attach closures to events on elements ||
44+
45+
These web-specific examples must be run with `dioxus-cli` using `dioxus develop --example XYZ`
46+
47+
| Example | What it does |
48+
| ------- | ------------ |
49+
| asd | this does |
50+
| asd | this does |

examples/listener.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ fn main() {}
2121
/// Antipattern: Iterators without keys
2222
/// -----------------------------------
2323
///
24-
/// This is considered an anti-pattern for performance reasons. Dioxus must diff your current and old layout and must
24+
/// This is considered an anti-pattern for performance reasons. Dioxus will diff your current and old layout and must
2525
/// take a slower path if it can't correlate old elements with new elements. Lists are particularly susceptible to the
2626
/// "slow" path, so you're strongly encouraged to provide a unique ID stable between renders. Additionally, providing
27-
/// the *wrong* keys is even worse. Keys should be:
27+
/// the *wrong* keys is even worse - props might be assigned to the wrong components! Keys should be:
2828
/// - Unique
2929
/// - Stable
3030
/// - Predictable
@@ -39,7 +39,7 @@ static AntipatternNoKeys: FC<NoKeysProps> = |cx| {
3939
rsx!(in cx, ul {
4040
{cx.data.iter().map(|(k, v)| rsx!(li { "List item: {v}" }))}
4141
});
42-
// Like this:
42+
// RIGHT: Like this:
4343
rsx!(in cx, ul {
4444
{cx.data.iter().map(|(k, v)| rsx!(li { key: "{k}", "List item: {v}" }))}
4545
})

examples/reference/async.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use dioxus::prelude::*;
2+
fn main() {}
3+
4+
static Example: FC<()> = |cx| {
5+
cx.render(rsx! {
6+
div {
7+
8+
}
9+
})
10+
};

examples/reference/basics.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use dioxus::prelude::*;
2+
fn main() {}
3+
4+
static Example: FC<()> = |cx| {
5+
cx.render(rsx! {
6+
div {
7+
8+
}
9+
})
10+
};

examples/reference/children.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! Example: Children
2+
//! -----------------
3+
//!
4+
//! Dioxus supports passing children in from the parent. These children are allocated in the parent and just forced
5+
//! into the child. Components that pass in children may not be safely memoized, though in practice its rare for a
6+
//! change in a parent to not result in a different set of children.
7+
//!
8+
//! In Dioxus, children can *only be a list*. Unlike React, you cannot pass in functions or arbitrary data. This is
9+
//! partially a limitaiton in having static types, but is rather intentional to encourage the use of attributes where
10+
//! arbitrary child data might normally be used. Check out the `function driven children` example for how to adopt your
11+
//! React pattern for Dioxus' semantics.
12+
//!
13+
//! Dioxus will let you use the `children` method more than once - and it's semantically *okay* - but you'll likely
14+
//! ruin your page if you try to clone elements in this way. Under the hood, Dioxus shares a "mounted ID" for each node,
15+
//! and mounting the same VNode in two places will overwrite the first mounted ID. This will likely lead to dead elements.
16+
//!
17+
//! In the future, this might become a runtime error, so consider it an error today.
18+
19+
use dioxus::prelude::*;
20+
fn main() {}
21+
22+
static App: FC<()> = |cx| {
23+
cx.render(rsx! {
24+
div {
25+
Banner {
26+
p { "Some Content1" }
27+
}
28+
Banner {
29+
p { "Some Content2" }
30+
}
31+
}
32+
})
33+
};
34+
35+
static Banner: FC<()> = |cx| {
36+
cx.render(rsx! {
37+
div {
38+
h1 { "This is a great banner!" }
39+
div { class: "content"
40+
{cx.children()}
41+
}
42+
footer { "Wow, what a great footer" }
43+
}
44+
})
45+
};
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//! Example: Conditional Rendering
2+
//! ------------------------------
3+
//!
4+
//! This example shows how to hide or show elements using conditional rendering.
5+
//!
6+
//! Often times you might want to display a different UI given some sort of condition. This is called "conditonal rendering".
7+
//! In Dioxus, you can perform conditional rendering with optionals or matching.
8+
//!
9+
//! The rsx! and html! macro accepts anything that is `IntoIter<Item = impl IntoVnode>`. Options and Results both implement
10+
//! IntoIter for impl VNode, so you can use option/result for conditional rendering.
11+
12+
use dioxus::prelude::*;
13+
14+
fn main() {}
15+
16+
// Convert a boolean conditional into a hide/show
17+
#[derive(PartialEq, Props)]
18+
struct MyProps {
19+
should_show: bool,
20+
}
21+
static Example: FC<MyProps> = |cx| {
22+
cx.render(rsx! {
23+
div {
24+
{cx.should_show.then(|| rsx!{
25+
h1 { "showing the title!" }
26+
})}
27+
}
28+
})
29+
};
30+
31+
// Convert a boolean conditional into an either/or
32+
// Because rsx! is lazy (produces a closure), we cannot use it in two branch arms. To use it in matching/branching, we
33+
// must render it.
34+
//
35+
// Dioxus will let you render any `LazyNodes` into a `VNode` with `cx.render`. `rsx!` also supports the `in cx` syntax
36+
// which will do essentially the same thing as `cx.render`.
37+
//
38+
// In short:
39+
// `rsx!(in cx, ...)` is shorthand for `cx.render(rsx!(...))`
40+
#[derive(PartialEq, Props)]
41+
struct MyProps1 {
42+
should_show: bool,
43+
}
44+
static Example1: FC<MyProps1> = |cx| {
45+
cx.render(rsx! {
46+
div {
47+
// With matching
48+
{match cx.should_show {
49+
true => cx.render(rsx!(div {"it is true!"})),
50+
false => rsx!(in cx, div {"it is false!"}),
51+
}}
52+
53+
// or with just regular conditions
54+
{if cx.should_show {
55+
rsx!(in cx, div {"it is true!"})
56+
} else {
57+
rsx!(in cx, div {"it is false!"})
58+
}}
59+
60+
// or with optional chaining
61+
{
62+
cx.should_show
63+
.then(|| rsx!(in cx, div {"it is false!"}))
64+
.unwrap_or_else(|| rsx!(in cx, div {"it is false!"}))
65+
}
66+
}
67+
})
68+
};
69+
70+
/// Matching can be expanded
71+
72+
#[derive(PartialEq)]
73+
enum Color {
74+
Green,
75+
Yellow,
76+
Red,
77+
}
78+
#[derive(PartialEq, Props)]
79+
struct MyProps2 {
80+
color: Color,
81+
}
82+
static Example2: FC<MyProps2> = |cx| {
83+
cx.render(rsx! {
84+
div {
85+
{match cx.color {
86+
Color::Green => rsx!(in cx, div {"it is Green!"}),
87+
Color::Yellow => rsx!(in cx, div {"it is Yellow!"}),
88+
Color::Red => rsx!(in cx, div {"it is Red!"}),
89+
}}
90+
}
91+
})
92+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use dioxus::prelude::*;
2+
fn main() {}
3+
4+
static Example: FC<()> = |cx| {
5+
cx.render(rsx! {
6+
div {
7+
8+
}
9+
})
10+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use dioxus::prelude::*;
2+
fn main() {}
3+
4+
static Example: FC<()> = |cx| {
5+
cx.render(rsx! {
6+
div {
7+
8+
}
9+
})
10+
};

0 commit comments

Comments
 (0)