Improve inline docs (#2460)
Improve inline docs * improve incorrect event handler return error message * Improve event handler docs * document the eval functions * document spawn and common spawn errors * fix event handler docs * add notes about how you use attributes and elements in rsx * add doc aliases for attributes and events we rename * add some more aliases for common search terms * don't doc ignore any public examples in core * don't ignore public doc examples in ssr * don't ignore examples in the dioxus package readme * add a warning when you launch without a renderer enabled * fix some outdated element docs * add a bunch of examples to resource * add notes about desktop events * add more docs for use_resource * add on_unimplemented hint to Dependency * fix some unresolved links * add examples to each of the router traits * add not implemented errors for router traits * add an example to the routable trait * expand rsx macro docs * improve memo docs * update the dioxus readme * mention dioxus crate features in the docs * fix a bunch of doc tests * fix html doc tests * fix router doc tests * fix dioxus signals doc tests * fix dioxus ssr doc tests * fix use_future example in the hooks cheat sheet * add a javascript alias for eval * fix hook explanation values * remove unused embed-doc-image dependency
This commit is contained in:
1 parent
054351139f
commit
0127501dbf
71 files changed
+4132
-1162
No files matched your search
@@ -4,25 +4,47 @@ use crate::innerlude::*;
|
||||
|
||||
/// Every "Props" used for a component must implement the `Properties` trait. This trait gives some hints to Dioxus
|
||||
/// on how to memoize the props and some additional optimizations that can be made. We strongly encourage using the
|
||||
/// derive macro to implement the `Properties` trait automatically as guarantee that your memoization strategy is safe.
|
||||
/// derive macro to implement the `Properties` trait automatically.
|
||||
///
|
||||
/// If your props are 'static, then Dioxus will require that they also be PartialEq for the derived memoize strategy.
|
||||
///
|
||||
/// By default, the memoization strategy is very conservative, but can be tuned to be more aggressive manually. However,
|
||||
/// this is only safe if the props are 'static - otherwise you might borrow references after-free.
|
||||
///
|
||||
/// We strongly suggest that any changes to memoization be done at the "PartialEq" level for 'static props. Additionally,
|
||||
/// we advise the use of smart pointers in cases where memoization is important.
|
||||
/// Dioxus requires your props to be 'static, `Clone`, and `PartialEq`. We use the `PartialEq` trait to determine if
|
||||
/// the props have changed when we diff the component.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// For props that are 'static:
|
||||
/// ```rust, ignore
|
||||
/// ```rust
|
||||
/// # use dioxus::prelude::*;
|
||||
/// #[derive(Props, PartialEq, Clone)]
|
||||
/// struct MyProps {
|
||||
/// struct MyComponentProps {
|
||||
/// data: String
|
||||
/// }
|
||||
///
|
||||
/// fn MyComponent(props: MyComponentProps) -> Element {
|
||||
/// rsx! {
|
||||
/// div { "Hello {props.data}" }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Or even better, derive your entire props struct with the [`#[crate::component]`] macro:
|
||||
///
|
||||
/// ```rust
|
||||
/// # use dioxus::prelude::*;
|
||||
/// #[component]
|
||||
/// fn MyComponent(data: String) -> Element {
|
||||
/// rsx! {
|
||||
/// div { "Hello {data}" }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[rustversion::attr(
|
||||
since(1.78.0),
|
||||
diagnostic::on_unimplemented(
|
||||
message = "`Props` is not implemented for `{Self}`",
|
||||
label = "Props",
|
||||
note = "Props is a trait that is automatically implemented for all structs that can be used as props for a component",
|
||||
note = "If you manually created a new properties struct, you may have forgotten to add `#[derive(Props, PartialEq, Clone)]` to your struct",
|
||||
)
|
||||
)]
|
||||
pub trait Properties: Clone + Sized + 'static {
|
||||
/// The type of the builder for this component.
|
||||
/// Used to create "in-progress" versions of the props.
|
||||
@@ -96,6 +118,28 @@ where
|
||||
}
|
||||
|
||||
/// Any component that implements the `ComponentFn` trait can be used as a component.
|
||||
///
|
||||
/// This trait is automatically implemented for functions that are in one of the following forms:
|
||||
/// - `fn() -> Element`
|
||||
/// - `fn(props: Properties) -> Element`
|
||||
///
|
||||
/// You can derive it automatically for any function with arguments that implement PartialEq with the `#[component]` attribute:
|
||||
/// ```rust
|
||||
/// # use dioxus::prelude::*;
|
||||
/// #[component]
|
||||
/// fn MyComponent(a: u32, b: u32) -> Element {
|
||||
/// rsx! { "a: {a}, b: {b}" }
|
||||
/// }
|
||||
/// ```
|
||||
#[rustversion::attr(
|
||||
since(1.78.0),
|
||||
diagnostic::on_unimplemented(
|
||||
message = "`Component<{Props}>` is not implemented for `{Self}`",
|
||||
label = "Component",
|
||||
note = "Components are functions in the form `fn() -> Element`, `fn(props: Properties) -> Element`, or `#[component] fn(partial_eq1: u32, partial_eq2: u32) -> Element`.",
|
||||
note = "You may have forgotten to add `#[component]` to your function to automatically implement the `ComponentFunction` trait."
|
||||
)
|
||||
)]
|
||||
pub trait ComponentFunction<Props, Marker = ()>: Clone + 'static {
|
||||
/// Get the type id of the component.
|
||||
fn id(&self) -> TypeId {
|
||||
|
||||
Reference in new issue
Block a user