-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to convert closure into Callback? #559
Comments
You're pretty close, I think you want to do this: onsignal = |msg: MapMsg| AppMsg::HandleMapMsg(msg) Or more simply: onsignal = AppMsg::HandleMapMsg |
Thanks @jstarry It works but I have two questions:
|number: i32| number + 1
|number: i32| -> i32 { number + 1 } So why does one work in that context but not the other? |
No problem, thanks for your questions:
|
@jstarry I have a use case where the callback is actually optional. How would I go about this in version 0.8? |
@Wodann you will probably want something like this (untested) #[derive(Properties)]
pub struct Props {
optional_cb: Option<Callback<Msg>>
}
...
html! {
<Component optional_cb=Some(|_| Msg::Callback) />
} |
That's what I expected too, but if I do it according to that format:
I get the following compile error:
|
@Wodann ah it's because we have special handling for transforming closures into callbacks: https://github.com/yewstack/yew/blob/master/src/virtual_dom/vcomp.rs#L143 sorry for misleading you! Seems like we could add a transformer for optional callbacks too! Feel free to add if you have time: #609 For now, you can use this workaround: pub struct Callbacks {
on_click: Callback<(u32, u32)>,
}
pub struct Model {
props: Props,
callbacks: Callbacks,
}
// ...
fn create(props: Self::Properties, mut link: ComponentLink<Self>) -> Self {
let callbacks = Callbacks {
on_click: link.send_back(|cell| Msg::ClickCell(cell)),
};
Model {
props,
callbacks,
}
}
// ...
fn render(&self) -> Html<Self> {
html! {
<Child on_click=Some(self.callbacks.on_click.clone()) />
}
} |
Description
I'm submitting a question (check one with "x")
I need to send messages between my components.
In #270 @hgzimmerman explained that the easiest way for this is to do it with callbacks like in the
custom_components
example. However, instead of sending nothing (()
), I want to send back specific messages.Expected Results
Here are the relevant snippets, the full code can be found here. The problem is with the
Callback::from()
call.I also tried the following which also did not work:
Actual Results
I get the following error, even though I explicitely specify the return value of the closure.
Context (Environment)
The text was updated successfully, but these errors were encountered: