dioxus/reference/antipatterns.rs

182 lines
8.5 KiB
Rust
Raw Normal View History

//! Example: Antipatterns
//! ---------------------
//!
//! This example shows what *not* to do and provides a reason why a given pattern is considered an "AntiPattern". Most
2021-10-24 17:30:36 +00:00
//! anti-patterns are considered wrong due performance reasons or violate the "rules" of Dioxus. These rules are
//! borrowed from other successful UI frameworks, and Dioxus is more focused on providing a familiar, ergonomic interface
//! rather than building new harder-to-misuse patterns.
2021-06-24 03:25:34 +00:00
//!
//! In this list we showcase:
//! - Not adding keys for iterators
//! - Heavily nested fragments
2021-10-24 17:30:36 +00:00
//! - Understanding ordering of set_state
2021-06-24 03:25:34 +00:00
//! - Naming conventions
//! - Rules of hooks
//!
//! Feel free to file a PR or Issue if you run into another antipattern that you think users of Dioxus should know about.
use dioxus::prelude::*;
2021-06-24 03:25:34 +00:00
/// Antipattern: Iterators without keys
/// -----------------------------------
///
2021-07-02 05:30:52 +00:00
/// This is considered an anti-pattern for performance reasons. Dioxus will diff your current and old layout and must
/// take a slower path if it can't correlate old elements with new elements. Lists are particularly susceptible to the
2021-10-24 17:30:36 +00:00
/// "slow" path, so you're strongly encouraged to provide a unique, stable ID between renders. Additionally, providing
2021-07-02 05:30:52 +00:00
/// the *wrong* keys is even worse - props might be assigned to the wrong components! Keys should be:
2021-06-24 03:25:34 +00:00
/// - Unique
/// - Stable
/// - Predictable
///
/// Dioxus will log an error in the console if it detects that your iterator does not properly generate keys
#[derive(PartialEq, Props)]
struct NoKeysProps {
2021-06-24 03:25:34 +00:00
data: std::collections::HashMap<u32, String>,
}
2021-10-16 21:37:28 +00:00
static AntipatternNoKeys: FC<NoKeysProps> = |(cx, props)| {
// WRONG: Make sure to add keys!
2021-09-16 17:20:04 +00:00
rsx!(cx, ul {
2021-09-21 17:42:52 +00:00
{props.data.iter().map(|(k, v)| rsx!(li { "List item: {v}" }))}
});
2021-07-02 05:30:52 +00:00
// RIGHT: Like this:
2021-09-16 17:20:04 +00:00
rsx!(cx, ul {
2021-09-21 17:42:52 +00:00
{props.data.iter().map(|(k, v)| rsx!(li { key: "{k}", "List item: {v}" }))}
})
};
/// Antipattern: Deeply nested fragments
/// ------------------------------------
///
/// This particular antipattern is not necessarily an antipattern in other frameworks but does has a performance impact
2021-10-24 17:30:36 +00:00
/// in Dioxus apps. Fragments don't mount a physical element to the DOM immediately, so Dioxus must recurse into its
/// children to find a physical DOM node. This process is called "normalization". Other frameworks perform an agressive
/// mutative normalization while Dioxus keeps your VNodes immutable. This means that deeply nested fragments make Dioxus
/// perform unnecessary work. Prefer one or two levels of fragments / nested components until presenting a true DOM element.
///
/// 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.
2021-10-16 21:37:28 +00:00
static AntipatternNestedFragments: FC<()> = |(cx, props)| {
2021-06-24 03:25:34 +00:00
// Try to avoid heavily nesting fragments
2021-09-16 17:20:04 +00:00
rsx!(cx,
Fragment {
Fragment {
Fragment {
Fragment {
Fragment {
div { "Finally have a real node!" }
}
}
}
}
}
)
};
2021-06-24 03:25:34 +00:00
2021-10-24 17:30:36 +00:00
/// Antipattern: Using state after it's been updated
2021-06-24 03:25:34 +00:00
/// -----------------------------------------------
///
/// This is an antipattern in other frameworks, but less so in Dioxus. However, it's important to highlight that use_state
/// does *not* work the same way as it does in React. Rust provides explicit guards against mutating shared data - a huge
/// problem in JavaScript land. With Rust and Dioxus, it's nearly impossible to misuse `use_state` - you simply can't
/// accidentally modify the state you've received!
///
/// 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.
2021-10-24 17:30:36 +00:00
static AntipatternRelyingOnSetState: FC<()> = |(cx, props)| {
2021-07-11 22:39:45 +00:00
let (state, set_state) = use_state(cx, || "Hello world").classic();
2021-06-24 03:25:34 +00:00
set_state("New state");
// This will return false! `state` will *still* be "Hello world"
assert!(state == &"New state");
todo!()
};
/// Antipattern: Capitalization
/// ---------------------------
///
/// This antipattern is enforced to retain parity with other frameworks and provide useful IDE feedback, but is less
2021-10-24 17:30:36 +00:00
/// critical than other potential misuses. In short:
2021-06-24 03:25:34 +00:00
/// - Only raw elements may start with a lowercase character
/// - All components must start with an uppercase character
///
2021-10-24 17:30:36 +00:00
/// i.e.: the following component will be rejected when attempted to be used in the rsx! macro
2021-10-16 21:37:28 +00:00
static antipattern_component: FC<()> = |(cx, props)| todo!();
2021-06-24 03:25:34 +00:00
/// Antipattern: Misusing hooks
/// ---------------------------
///
2021-10-24 17:30:36 +00:00
/// This pattern is an unfortunate one where Dioxus replicates the same behavior as other frameworks. Dioxus supports
/// "hooks" - i.e. "memory cells" that allow a value to be stored between renders. This allows other hooks to tap into
/// a component's "memory" without explicitly adding all of its data to a struct definition. In Dioxus, hooks are allocated
2021-06-24 03:25:34 +00:00
/// with a bump arena and then immediately sealed.
///
2021-10-24 17:30:36 +00:00
/// This means that hooks may not be misused:
2021-06-24 03:25:34 +00:00
/// - Called out of order
/// - Called in a conditional
/// - Called in loops or callbacks
///
/// For the most part, Rust helps with rule #3 but does not save you from misusing rule #1 or #2. Dioxus will panic
2021-10-24 17:30:36 +00:00
/// if hooks do not downcast to the same data between renders. This is validated by TypeId and, eventually, a custom key.
2021-06-24 03:25:34 +00:00
#[derive(PartialEq, Props)]
struct MisuedHooksProps {
should_render_state: bool,
}
2021-10-16 21:37:28 +00:00
static AntipatternMisusedHooks: FC<MisuedHooksProps> = |(cx, props)| {
2021-09-21 17:42:52 +00:00
if props.should_render_state {
2021-06-24 03:25:34 +00:00
// do not place a hook in the conditional!
// prefer to move it out of the conditional
2021-07-11 22:39:45 +00:00
let (state, set_state) = use_state(cx, || "hello world").classic();
2021-09-16 17:20:04 +00:00
rsx!(cx, div { "{state}" })
2021-06-24 03:25:34 +00:00
} else {
2021-09-16 17:20:04 +00:00
rsx!(cx, div { "Not rendering state" })
2021-06-24 03:25:34 +00:00
}
};
2021-06-24 04:18:29 +00:00
2021-10-24 17:30:36 +00:00
/// Antipattern: Downcasting refs and panicking
2021-06-24 04:18:29 +00:00
/// ------------------------------------------
///
2021-10-24 17:30:36 +00:00
/// Occasionally it's useful to get the ref of an element to handle it directly. Elements support downcasting to
/// Dioxus' virtual element types as well as their true native counterparts. Downcasting to Dioxus' virtual elements
2021-06-24 04:18:29 +00:00
/// will never panic, but downcasting to native elements will fail if on an unsupported platform. We recommend avoiding
2021-10-24 17:30:36 +00:00
/// publishing hooks and components that deeply rely on controlling elements using their native `ref`, preferring to
2021-06-24 04:18:29 +00:00
/// use their Dioxus Virtual Element counterpart instead.
2021-10-24 17:30:36 +00:00
/// This particular code *will panic* due to the unwrap. Try to avoid these types of patterns.
2021-06-24 04:18:29 +00:00
/// ---------------------------------
/// TODO: Get this to compile properly
/// let div_ref = use_node_ref(&cx);
///
/// cx.render(rsx!{
/// div { ref: div_ref, class: "custom class",
/// button { "click me to see my parent's class"
/// onclick: move |_| if let Some(div_ref) = div_ref {
/// panic!("Div class is {}", div_ref.to_native::<web_sys::Element>().unwrap().class())
/// }
/// }
/// }
/// })
2021-10-16 21:37:28 +00:00
static _example: FC<()> = |(cx, props)| todo!();
2021-06-24 04:18:29 +00:00
/// Antipattern: publishing components and hooks with all features enabled
/// ----------------------------------------------------------------------
///
/// The `dioxus` crate combines a bunch of useful utilities together (like the rsx! and html! macros, hooks, and more).
/// However, when publishing your custom hook or component, we highly advise using only the `core` feature on the dioxus
/// crate. This makes your crate compile faster, makes it more stable, and avoids bringing in incompatible libraries that
/// might make it not compile on unsupported platforms.
///
/// We don't have a code snippet for this, but just prefer to use this line:
/// dioxus = { version = "*", features = ["core"]}
/// instead of this one:
/// dioxus = { version = "*", features = ["web", "desktop", "full"]}
/// in your Cargo.toml
///
/// This will only include the `core` dioxus crate which is relatively slim and fast to compile and avoids target-specific
/// libraries.
2021-10-16 21:37:28 +00:00
static __example: FC<()> = |(cx, props)| todo!();
2021-07-16 20:11:25 +00:00
2021-10-16 21:37:28 +00:00
pub static Example: FC<()> = |(cx, props)| {
2021-07-16 20:11:25 +00:00
cx.render(rsx! {
AntipatternNoKeys { data: std::collections::HashMap::new() }
AntipatternNestedFragments {}
})
};