mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
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:
parent
189a7296d1
commit
30e2d548ef
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user