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

Use keyed lists in tutorial example #2948

Merged
merged 4 commits into from Nov 8, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions website/docs/tutorial/index.mdx
Expand Up @@ -243,10 +243,14 @@ mapping it to `html!` and collecting it as `Html`:

```rust ,ignore
let videos = videos.iter().map(|video| html! {
<p>{format!("{}: {}", video.speaker, video.title)}</p>
<p key={video.id}>{format!("{}: {}", video.speaker, video.title)}</p>
}).collect::<Html>();
```

:::tip
Keys on list items helps Yew keep track of which items have changed in the list, resulting in faster re-renders. [It is always recommended to use keys in lists](/concepts/html/lists.mdx#keyed-lists).
:::

And finally we need to replace the hardcoded list of videos with the `Html` we created from data:

```rust ,ignore {6-10}
Expand Down Expand Up @@ -301,7 +305,7 @@ struct VideosListProps {
#[function_component(VideosList)]
fn videos_list(VideosListProps { videos }: &VideosListProps) -> Html {
videos.iter().map(|video| html! {
<p>{format!("{}: {}", video.speaker, video.title)}</p>
<p key={video.id}>{format!("{}: {}", video.speaker, video.title)}</p>
}).collect()
}
```
Expand Down Expand Up @@ -333,7 +337,7 @@ Now, we can update our `App` component to make use of `VideosList` component.
fn app() -> Html {
// ...
- let videos = videos.iter().map(|video| html! {
- <p>{format!("{}: {}", video.speaker, video.title)}</p>
- <p key={video.id}>{format!("{}: {}", video.speaker, video.title)}</p>
- }).collect::<Html>();
-
html! {
Expand Down