dioxus/docs/main-concepts/06-subscription-api.md

34 lines
1 KiB
Markdown
Raw Normal View History

2021-06-25 21:15:33 -04:00
# Subscriptions
2021-01-20 12:04:27 -05:00
Yew subscriptions are used to schedule update for components into the future. The `Context` object can create subscriptions:
```rust
2021-07-18 12:39:32 -04:00
fn Component(cx: Component<()>) -> DomTree {
2021-06-25 21:15:33 -04:00
let update = cx.schedule();
2021-01-20 12:04:27 -05:00
// Now, when the subscription is called, the component will be re-evaluted
update.consume();
}
```
Whenever a component's subscription is called, the component will then re-evaluated. You can consider the input properties of
a component to be just another form of subscription. By default, the Dioxus component system automatically diffs a component's props
2021-06-25 21:15:33 -04:00
when the parent function is called, and if the props are different, the child component's subscription is called.
2021-01-20 12:04:27 -05:00
The subscription API exposes this functionality allowing hooks and state management solutions the ability to update components whenever
some state or event occurs outside of the component. For instance, the `use_context` hook uses this to subscribe components that use a
particular context.
```rust
2021-06-25 21:15:33 -04:00
fn use_context<I>(cx: Context<T>) -> I {
2021-01-20 12:04:27 -05:00
}
```