Custom event in custom components ? #359
-
Is it possible to define a custom event for a component ? I mean, if I define a Board component, can I dispatch an "signal" named "Won" ? #[derive(PartialEq, Props)]
pub struct BoardProps {
cols_count: u8,
}
#[allow(non_snake_case)]
pub fn Board<'a>(cx: Scope<'a, BoardProps>) -> Element {
cx.render(rsx!(
div {
class: "board",
// Is there something for dispatching an event and its parameters so that a parent component can
// listen to "Won" event ?
onclick: move |_| dispatch('Won', {white: true});
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
jkelleyrtp
Apr 18, 2022
Replies: 1 comment 2 replies
-
While there's not really a way of "emitting" into parent events, you can just pass a handler in through BoardProps called "on_won" and then call it with whatever event you want. https://dioxuslabs.com/guide/components/component_children.html#passing-handlers I think this solves your use case - you want handlers above to be passed into components. |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
loloof64
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While there's not really a way of "emitting" into parent events, you can just pass a handler in through BoardProps called "on_won" and then call it with whatever event you want.
https://dioxuslabs.com/guide/components/component_children.html#passing-handlers
I think this solves your use case - you want handlers above to be passed into components.