fix: readme and examples syntax
This commit is contained in:
1 parent
f24aae8787
commit
3dc0e59876
102 files changed
+213
-1222
No files matched your search
@@ -54,7 +54,7 @@ static AntipatternNoKeys: Component<NoKeysProps> = |cx| {
|
||||
///
|
||||
/// Only Component and Fragment nodes are susceptible to this issue. Dioxus mitigates this with components by providing
|
||||
/// an API for registering shared state without the ContextProvider pattern.
|
||||
static AntipatternNestedFragments: Component<()> = |cx| {
|
||||
static AntipatternNestedFragments: Component = |cx| {
|
||||
// Try to avoid heavily nesting fragments
|
||||
rsx!(cx,
|
||||
Fragment {
|
||||
@@ -82,7 +82,7 @@ static AntipatternNestedFragments: Component<()> = |cx| {
|
||||
/// However, calling set_state will *not* update the current version of state in the component. This should be easy to
|
||||
/// recognize from the function signature, but Dioxus will not update the "live" version of state. Calling `set_state`
|
||||
/// merely places a new value in the queue and schedules the component for a future update.
|
||||
static AntipatternRelyingOnSetState: Component<()> = |cx| {
|
||||
static AntipatternRelyingOnSetState: Component = |cx| {
|
||||
let (state, set_state) = use_state(&cx, || "Hello world").classic();
|
||||
set_state("New state");
|
||||
// This will return false! `state` will *still* be "Hello world"
|
||||
@@ -99,7 +99,7 @@ static AntipatternRelyingOnSetState: Component<()> = |cx| {
|
||||
/// - All components must start with an uppercase character
|
||||
///
|
||||
/// i.e.: the following component will be rejected when attempted to be used in the rsx! macro
|
||||
static antipattern_component: Component<()> = |cx, props| todo!();
|
||||
static antipattern_component: Component = |cx| todo!();
|
||||
|
||||
/// Antipattern: Misusing hooks
|
||||
/// ---------------------------
|
||||
@@ -153,7 +153,7 @@ static AntipatternMisusedHooks: Component<MisuedHooksProps> = |cx| {
|
||||
/// }
|
||||
/// }
|
||||
/// })
|
||||
static _example: Component<()> = |cx, props| todo!();
|
||||
static _example: Component = |cx| todo!();
|
||||
|
||||
/// Antipattern: publishing components and hooks with all features enabled
|
||||
/// ----------------------------------------------------------------------
|
||||
@@ -171,9 +171,9 @@ static _example: Component<()> = |cx, props| todo!();
|
||||
///
|
||||
/// This will only include the `core` dioxus crate which is relatively slim and fast to compile and avoids target-specific
|
||||
/// libraries.
|
||||
static __example: Component<()> = |cx, props| todo!();
|
||||
static __example: Component = |cx| todo!();
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
AntipatternNoKeys { data: std::collections::HashMap::new() }
|
||||
AntipatternNestedFragments {}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
use dioxus::prelude::*;
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
Greeting {
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
use dioxus::prelude::*;
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
Banner {
|
||||
@@ -31,7 +31,7 @@ pub static Example: Component<()> = |cx| {
|
||||
})
|
||||
};
|
||||
|
||||
pub static Banner: Component<()> = |cx| {
|
||||
pub static Banner: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
h1 { "This is a great banner!" }
|
||||
|
||||
@@ -89,7 +89,7 @@ pub static Example2: Component<MyProps2> = |cx| {
|
||||
})
|
||||
};
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
let should_show = use_state(&cx, || false);
|
||||
let mut color_index = use_state(&cx, || 0);
|
||||
let color = match *color_index % 2 {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use dioxus::prelude::*;
|
||||
fn main() {}
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
|
||||
@@ -10,7 +10,7 @@ pub static Example: Component<()> = |cx| {
|
||||
};
|
||||
|
||||
// A controlled component:
|
||||
static ControlledSelect: Component<()> = |cx| {
|
||||
static ControlledSelect: Component = |cx| {
|
||||
let value = use_state(&cx, || String::from("Grapefruit"));
|
||||
cx.render(rsx! {
|
||||
select { value: "{value}", onchange: move |evt| value.set(evt.value()),
|
||||
@@ -23,7 +23,7 @@ static ControlledSelect: Component<()> = |cx| {
|
||||
};
|
||||
|
||||
// TODO - how do uncontrolled things work?
|
||||
static UncontrolledSelect: Component<()> = |cx| {
|
||||
static UncontrolledSelect: Component = |cx| {
|
||||
let value = use_state(&cx, || String::new());
|
||||
|
||||
cx.render(rsx! {
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
use dioxus::prelude::*;
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
custom_element {
|
||||
|
||||
@@ -5,4 +5,4 @@
|
||||
|
||||
use dioxus::prelude::*;
|
||||
|
||||
pub static Example: Component<()> = |cx, props| cx.render(rsx! { Fragment {} });
|
||||
pub static Example: Component = |cx| cx.render(rsx! { Fragment {} });
|
||||
@@ -23,14 +23,14 @@ fn main() {}
|
||||
/// This is one way to go about error handling (just toss things away with unwrap).
|
||||
/// However, if you get it wrong, the whole app will crash.
|
||||
/// This is pretty flimsy.
|
||||
static App: Component<()> = |cx| {
|
||||
static App: Component = |cx| {
|
||||
let data = get_data().unwrap();
|
||||
cx.render(rsx!( div { "{data}" } ))
|
||||
};
|
||||
|
||||
/// This is a pretty verbose way of error handling
|
||||
/// However, it's still pretty good since we don't panic, just fail to render anything
|
||||
static App1: Component<()> = |cx| {
|
||||
static App1: Component = |cx| {
|
||||
let data = match get_data() {
|
||||
Some(data) => data,
|
||||
None => return None,
|
||||
@@ -46,7 +46,7 @@ static App1: Component<()> = |cx| {
|
||||
/// a user is logged in.
|
||||
///
|
||||
/// Dioxus will throw an error in the console if the None-path is ever taken.
|
||||
static App2: Component<()> = |cx| {
|
||||
static App2: Component = |cx| {
|
||||
let data = get_data()?;
|
||||
cx.render(rsx!( div { "{data}" } ))
|
||||
};
|
||||
@@ -54,14 +54,14 @@ static App2: Component<()> = |cx| {
|
||||
/// This is top-tier error handling since it displays a failure state.
|
||||
///
|
||||
/// However, the error is lacking in context.
|
||||
static App3: Component<()> = |cx, props| match get_data() {
|
||||
static App3: Component = |cx| match get_data() {
|
||||
Some(data) => cx.render(rsx!( div { "{data}" } )),
|
||||
None => cx.render(rsx!( div { "Failed to load data :(" } )),
|
||||
};
|
||||
|
||||
/// For errors that return results, it's possible to short-circuit the match-based error handling with `.ok()` which converts
|
||||
/// a Result<T, V> into an Option<T> and lets you abort rendering by early-returning `None`
|
||||
static App4: Component<()> = |cx| {
|
||||
static App4: Component = |cx| {
|
||||
let data = get_data_err().ok()?;
|
||||
cx.render(rsx!( div { "{data}" } ))
|
||||
};
|
||||
@@ -69,7 +69,7 @@ static App4: Component<()> = |cx| {
|
||||
/// This is great error handling since it displays a failure state... with context!
|
||||
///
|
||||
/// Hopefully you'll never need to display a screen like this. It's rather bad taste
|
||||
static App5: Component<()> = |cx, props| match get_data_err() {
|
||||
static App5: Component = |cx| match get_data_err() {
|
||||
Ok(data) => cx.render(rsx!( div { "{data}" } )),
|
||||
Err(c) => cx.render(rsx!( div { "Failed to load data: {c}" } )),
|
||||
};
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
// Returning multiple elements with rsx! or html!
|
||||
static App1: Component<()> = |cx| {
|
||||
static App1: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
h1 { }
|
||||
h2 { }
|
||||
@@ -20,7 +20,7 @@ static App1: Component<()> = |cx| {
|
||||
};
|
||||
|
||||
// Using the Fragment component
|
||||
static App2: Component<()> = |cx| {
|
||||
static App2: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
Fragment {
|
||||
div {}
|
||||
@@ -31,7 +31,7 @@ static App2: Component<()> = |cx| {
|
||||
};
|
||||
|
||||
// Using the `fragment` method on the NodeFactory
|
||||
static App3: Component<()> = |cx| {
|
||||
static App3: Component = |cx| {
|
||||
cx.render(LazyNodes::new(move |fac| {
|
||||
fac.fragment_from_iter([
|
||||
fac.text(format_args!("A")),
|
||||
@@ -42,7 +42,7 @@ static App3: Component<()> = |cx| {
|
||||
}))
|
||||
};
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
App1 {}
|
||||
App2 {}
|
||||
|
||||
@@ -19,7 +19,7 @@ h1 {color: blue;}
|
||||
p {color: red;}
|
||||
"#;
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
head { style { "{STYLE}" } }
|
||||
body {
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
use dioxus::prelude::*;
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
head {
|
||||
style: { background_color: "powderblue" }
|
||||
@@ -29,7 +29,7 @@ pub static Example: Component<()> = |cx| {
|
||||
// .... technically the rsx! macro is slightly broken at the moment and allows styles not wrapped in style {}
|
||||
// I haven't noticed any name collisions yet, and am tentatively leaving this behavior in..
|
||||
// Don't rely on it.
|
||||
static Example2: Component<()> = |cx| {
|
||||
static Example2: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
div { color: "red"
|
||||
"hello world!"
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
use dioxus::prelude::*;
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
let example_data = use_state(&cx, || 0);
|
||||
|
||||
let v = (0..10).map(|f| {
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
use dioxus::prelude::*;
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
ButtonList {}
|
||||
NonUpdatingEvents {}
|
||||
@@ -16,7 +16,7 @@ pub static Example: Component<()> = |cx| {
|
||||
};
|
||||
|
||||
/// We can use `set_name` in multiple closures; the closures automatically *copy* the reference to set_name.
|
||||
static ButtonList: Component<()> = |cx| {
|
||||
static ButtonList: Component = |cx| {
|
||||
let name = use_state(&cx, || "...?");
|
||||
|
||||
let names = ["jack", "jill", "john", "jane"]
|
||||
@@ -33,7 +33,7 @@ static ButtonList: Component<()> = |cx| {
|
||||
|
||||
/// This shows how listeners may be without a visible change in the display.
|
||||
/// Check the console.
|
||||
static NonUpdatingEvents: Component<()> = |cx| {
|
||||
static NonUpdatingEvents: Component = |cx| {
|
||||
rsx!(cx, div {
|
||||
button {
|
||||
onclick: move |_| log::info!("Did not cause any updates!")
|
||||
@@ -42,7 +42,7 @@ static NonUpdatingEvents: Component<()> = |cx| {
|
||||
})
|
||||
};
|
||||
|
||||
static DisablePropagation: Component<()> = |cx| {
|
||||
static DisablePropagation: Component = |cx| {
|
||||
rsx!(cx,
|
||||
div {
|
||||
onclick: move |_| log::info!("event propagated to the div!")
|
||||
|
||||
@@ -21,7 +21,7 @@ use dioxus::prelude::*;
|
||||
|
||||
// By default, components with no props are always memoized.
|
||||
// A props of () is considered empty.
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
div { "100% memoized!" }
|
||||
})
|
||||
@@ -61,9 +61,9 @@ pub struct MyProps3<'a> {
|
||||
name: &'a str,
|
||||
}
|
||||
// We need to manually specify a lifetime that ensures props and scope (the component's state) share the same lifetime.
|
||||
// Using the `pub static Example: Component<()>` pattern _will_ specify a lifetime, but that lifetime will be static which might
|
||||
// Using the `pub static Example: Component` pattern _will_ specify a lifetime, but that lifetime will be static which might
|
||||
// not exactly be what you want
|
||||
fn Example3<'a>(cx: Context<'a>, props: &'a MyProps3) -> DomTree<'a> {
|
||||
fn Example3(cx: Scope<'a, MyProps3<'a>>) -> DomTree {
|
||||
cx.render(rsx! {
|
||||
div { "Not memoized! {cx.props.name}" }
|
||||
})
|
||||
@@ -77,7 +77,7 @@ pub struct MyProps4<'a> {
|
||||
}
|
||||
|
||||
// We need to manually specify a lifetime that ensures props and scope (the component's state) share the same lifetime.
|
||||
fn Example4<'a>(cx: Context<'a>, props: &'a MyProps4) -> DomTree<'a> {
|
||||
fn Example4<'a>(cx: Scope<'a, MyProps4<'a>>) -> DomTree {
|
||||
cx.render(rsx! {
|
||||
div { "Not memoized!", onclick: move |_| (props.onhandle)() }
|
||||
})
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use dioxus::prelude::*;
|
||||
fn main() {}
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
let p = 10;
|
||||
|
||||
cx.render(rsx! {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use dioxus::prelude::*;
|
||||
fn main() {}
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
use dioxus::prelude::*;
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
let props = MyProps {
|
||||
count: 0,
|
||||
live: true,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use dioxus::prelude::*;
|
||||
fn main() {}
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ struct DogApi {
|
||||
}
|
||||
const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random";
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
let doggo = use_suspense(
|
||||
cx,
|
||||
|| surf::get(ENDPOINT).recv_json::<DogApi>(),
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
use dioxus::prelude::*;
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
let count = use_state(&cx, || 0);
|
||||
let mut direction = use_state(&cx, || 1);
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use dioxus::prelude::*;
|
||||
use dioxus::ssr;
|
||||
|
||||
pub static Example: Component<()> = |cx| {
|
||||
pub static Example: Component = |cx| {
|
||||
let as_string = use_state(&cx, || {
|
||||
// Currently, SSR is only supported for whole VirtualDOMs
|
||||
// This is an easy/low hanging fruit to improve upon
|
||||
@@ -15,7 +15,7 @@ pub static Example: Component<()> = |cx| {
|
||||
})
|
||||
};
|
||||
|
||||
static SomeApp: Component<()> = |cx| {
|
||||
static SomeApp: Component = |cx| {
|
||||
cx.render(rsx! {
|
||||
div { style: {background_color: "blue"}
|
||||
h1 {"Some amazing app or component"}
|
||||
|
||||
Reference in new issue
Block a user