-
ProblemIn React, we can perform magic tricks with const Example = () => {
return (
<Trigger asChild>
<button className="myButton">
Click me
</button>
</Trigger>
);
} would produce the following DOM tree: <button {...props from Trigger} class="myButton">
Click me
</button> by doing something like this in const Trigger = ({
children,
asChild,
}: {
children: React.ReactNode;
asChild?: boolean;
}) => {
const [show, setShow] = React.useState(false);
if (asChild) {
return React.Children.only(
React.cloneElement(children as React.ReactElement, {
"data-hidden": !show,
onClick: () => setShow(true),
})
);
}
return <button onClick={() => setShow(true)}>{children}</button>;
}; QuestionDoes Dioxus provide a similar API for utilities like fn Example(cx: Scope) -> Element {
render! {
Trigger {
as_child: true,
class: "myButton",
button {
"Click me!"
}
}
}
} Based on my digging around it seems like this could be possible, but I didn't want to dive too deep before I made sure there's not an official way of achieving something similar. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
There has been some discussion about a VNode iterator in this issue, but would be more complicated than react because of the structure of VNodes that enables dioxus' improved performance. You can spread props pretty easily in the git version of dioxus. I think the API you posted is possible, but it would be much easier to support an API like this: fn Example(cx: Scope) -> Element {
render! {
button {
class: "myButton",
..create_trigger(),
"Click me!"
}
}
} |
Beta Was this translation helpful? Give feedback.
There has been some discussion about a VNode iterator in this issue, but would be more complicated than react because of the structure of VNodes that enables dioxus' improved performance.
You can spread props pretty easily in the git version of dioxus. I think the API you posted is possible, but it would be much easier to support an API like this: