Scope

Component’s Scope<_> API

The component “Scope“ is the mechanism through which components are able to create callbacks and update themselves using messages. We obtain a reference to this by calling link() on the context object passed to the component.

send_message

Sends a message to the component. Messages are handled by the update method which determines whether the component should re-render.

send_message_batch

Sends multiple messages to the component at the same time. This is similar to send_message but if any of the messages cause the update method to return true, the component will re-render after all messages in the batch have been processed.

If the given vector is empty, this function doesn’t do anything.

callback

Create a callback that will send a message to the component when it is executed. Under the hood, it will call send_message with the message returned by the provided closure.

  1. use yew::{html, Component, Context, Html};
  2. enum Msg {
  3. Text(String),
  4. }
  5. struct Comp;
  6. impl Component for Comp {
  7. type Message = Msg;
  8. type Properties = ();
  9. fn create(_ctx: &Context<Self>) -> Self {
  10. Self
  11. }
  12. fn view(&self, ctx: &Context<Self>) -> Html {
  13. // Create a callback that accepts some text and sends it
  14. // to the component as the `Msg::Text` message variant.
  15. let cb = ctx.link().callback(|text: String| Msg::Text(text));
  16. // The previous line is needlessly verbose to make it clearer.
  17. // It can be simplified it to this:
  18. let cb = ctx.link().callback(Msg::Text);
  19. // Will send `Msg::Text("Hello World!")` to the component.
  20. cb.emit("Hello World!".to_owned());
  21. html! {
  22. // html here
  23. }
  24. }
  25. }

batch_callback

Create a callback that will send a batch of messages to the component when it is executed. The difference to callback is that the closure passed to this method doesn’t have to return a message. Instead, the closure can return either Vec<Msg> or Option<Msg> where Msg is the component’s message type.

Vec<Msg> is treated as a batch of messages and uses send_message_batch under the hood.

Option<Msg> calls send_message if it is Some. If the value is None, nothing happens. This can be used in cases where, depending on the situation, an update isn’t required.

This is achieved using the SendAsMessage trait which is only implemented for these types. You can implement SendAsMessage for your own types which allows you to use them in batch_callback.