Added a note about using suspension (#3410)

* added a note about implementing suspending hooks

* fixed formatting

* extracted part of the note into a danger block

* added :::

* added spaces
This commit is contained in:
Tim Kurdov 2023-09-23 15:00:35 +01:00 committed by GitHub
parent 189a7296d1
commit 30e2d548ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -78,6 +78,23 @@ fn use_user() -> SuspensionResult<User> {
}
```
#### Note on implementing suspending hooks
[`Suspension::new`](https://docs.rs/yew/latest/yew/suspense/struct.Suspension.html#method.new) returns 2 values: the suspension context itself, and a suspension handle.
The latter is the one responsible for signaling when to re-render the suspended components, it provides 2 interchangable ways to do so:
1. Calling its [`resume`](https://docs.rs/yew/latest/yew/suspense/struct.SuspensionHandle.html#method.resume) method.
2. Dropping the handle.
:::danger
The suspension handle must be stored until it's time to update components, i.e. with newly received data;
otherwise, the suspended components will enter an infinite re-render loop, thus hampering performance.
In the example above, the suspension handle is preserved by being moved into a closure and passed into `on_load_user_complete`.
When the hypothetical user will be loaded, the closure will be called, thus calling `handle.resume()` and re-rendering the components associated with the suspension context.
:::
# Complete Example
```rust