mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-12-24 19:43:10 +00:00
508c560320
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.
83 lines
2.2 KiB
Rust
83 lines
2.2 KiB
Rust
#![allow(non_snake_case)]
|
|
use dioxus_core as dioxus;
|
|
use dioxus::{events::on::MouseEvent, prelude::*};
|
|
use dioxus_web::WebsysRenderer;
|
|
|
|
fn main() {
|
|
wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
|
|
console_error_panic_hook::set_once();
|
|
|
|
wasm_bindgen_futures::spawn_local(async {
|
|
let props = ExampleProps { initial_name: "..?"};
|
|
WebsysRenderer::new_with_props(Example, props)
|
|
.run()
|
|
.await
|
|
.unwrap()
|
|
});
|
|
}
|
|
|
|
#[derive(PartialEq, Props)]
|
|
struct ExampleProps {
|
|
initial_name: &'static str,
|
|
}
|
|
|
|
static Example: FC<ExampleProps> = |ctx| {
|
|
let name = use_state_new(&ctx, move || ctx.initial_name);
|
|
|
|
ctx.render(rsx! {
|
|
div {
|
|
class: "py-12 px-4 text-center w-full max-w-2xl mx-auto"
|
|
span {
|
|
class: "text-sm font-semibold"
|
|
"Dioxus Example: Jack and Jill"
|
|
}
|
|
h2 {
|
|
class: "text-5xl mt-2 mb-6 leading-tight font-semibold font-heading"
|
|
"Hello, {name}"
|
|
}
|
|
|
|
CustomButton { name: "Jack!", handler: move |_| name.set("Jack") }
|
|
CustomButton { name: "Jill!", handler: move |_| name.set("Jill") }
|
|
CustomButton { name: "Bob!", handler: move |_| name.set("Bob")}
|
|
Placeholder {val: name}
|
|
Placeholder {val: name}
|
|
}
|
|
})
|
|
};
|
|
|
|
|
|
|
|
#[derive(Props)]
|
|
struct ButtonProps<'src, F: Fn(MouseEvent)> {
|
|
name: &'src str,
|
|
handler: F
|
|
}
|
|
|
|
fn CustomButton<'b, 'a, F: Fn(MouseEvent)>(ctx: Context<'a>, props: &'b ButtonProps<'b, F>) -> VNode {
|
|
ctx.render(rsx!{
|
|
button {
|
|
class: "inline-block py-4 px-8 mr-6 leading-none text-white bg-indigo-600 hover:bg-indigo-900 font-semibold rounded shadow"
|
|
onmouseover: {&ctx.handler}
|
|
"{ctx.name}"
|
|
}
|
|
})
|
|
}
|
|
|
|
impl<F: Fn(MouseEvent)> PartialEq for ButtonProps<'_, F> {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
false
|
|
}
|
|
}
|
|
|
|
|
|
#[derive(Props, PartialEq)]
|
|
struct PlaceholderProps {
|
|
val: &'static str
|
|
}
|
|
fn Placeholder(ctx: Context, props: &PlaceholderProps) -> VNode {
|
|
ctx.render(rsx!{
|
|
div {
|
|
"child: {ctx.val}"
|
|
}
|
|
})
|
|
}
|