This change switches back to the original `ctx<props>` syntax for commponents. This lets lifetime elision to remove the need to match exactly which lifetime (props or ctx) gets carried to the output. As such, `Props` is currently required to be static. It *is* possible to loosen this restriction, and will be done in the future, though only through adding metadata about the props through the Props derive macro. Implementing the IS_STATIC trait is unsafe, so the derive macro will do it through some heuristics. For now, this unlocks sharing vnodes from parents to children, enabling pass-thru components, fragments, portals, etc.
1.6 KiB
Utilities
There are a few macros and utility functions that make life easier when writing Dioxus components.
The functional_component procedural macro
The functional_component proc macro allows you to inline props into the generic parameter of the component's context. This is useful When writing "pure" components, or when you don't want the extra clutter of structs, derives, and burden of naming things.
This macro allows allows a classic struct definition to be embedded directly into the function arguments. The props are automatically pulled from the context and destructured into the function's body, saving an extra step.
// Inlines and destructure props *automatically*
#[functional_component]
fn Example(ctx: Context, name: &str, pending: bool, count: i32 ) -> VNode {
html! {
<div>
<p> "Hello, {name}!" </p>
<p> "Status: {pending}!" </p>
<p> "Count {count}!" </p>
</div>
}
}
becomes this:
#[derive(Debug, Properties, PartialEq)]
struct ExampleProps {
name: String
pending: bool
count: i32
};
fn Example(ctx: &mut Context<ExampleProps>) -> VNode {
let ExampleProps {
name, pending, count
} = ctx.props();
rsx! {
<div>
<p> "Hello, {name}!" </p>
<p> "Status: {pending}!" </p>
<p> "Count {count}!" </p>
</div>
}
}
The rsx! macro
The rsx! macacro is similar to the html! macro in other libraries, but with a few add-ons to make it fun and easy to work with. We'll cover the rsx macro more in depth in the vnode-macro chapter.