Skip to content

Commit

Permalink
Added a note about using suspension (#3410)
Browse files Browse the repository at this point in the history
* added a note about implementing suspending hooks

* fixed formatting

* extracted part of the note into a danger block

* added :::

* added spaces
  • Loading branch information
its-the-shrimp authored Sep 23, 2023
1 parent 189a729 commit 30e2d54
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions website/docs/concepts/suspense.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 30e2d54

Please sign in to comment.