Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a note about using suspension #3410

Merged
merged 5 commits into from
Sep 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be a new line after this. See the link docs about using admonitions with prettier

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:::

An additional pair of colons are needed at the end of the admonitions.

:::

# Complete Example

```rust
Expand Down
Loading