Lists

Iterators

Yew supports two different syntaxes for building HTML from an iterator.

  • Syntax type 1
  • Syntax type 2

The first is to call collect::<Html>() on the final transform in your iterator, which returns a list that Yew can display.

  1. use yew::{html, Html};
  2. let items = (1..=10).collect::<Vec<_>>();
  3. html! {
  4. <ul class="item-list">
  5. { items.iter().collect::<Html>() }
  6. </ul>
  7. };

The alternative is to use the for keyword, which is not native Rust syntax and instead is used by the HTML macro to output the needed code to display the iterator.

  1. use yew::{html};
  2. let items = (1..=10).collect::<Vec<_>>();
  3. html! {
  4. <ul class="item-list">
  5. { for items.iter() }
  6. </ul>
  7. };

Keyed lists

A keyed list is a optimized list that has keys on all tags. key is a special prop provided by Yew which gives an html element a unique identifier which can be used for optimization purposes.

Lists - 图1警告

Key has to be unique and must not depend on the order of the list.

It is always recommended to add keys to lists.

Keys can be added by passing a unique value to the special key prop:

  1. use yew::html;
  2. let names = vec!["Sam","Bob","Ray"]
  3. html! {
  4. <div id="introductions">
  5. {
  6. names.into_iter().map(|name| {
  7. html!{<div key={name}>{ format!("Hello, I'am {}!",name) }</div>}
  8. }).collect::<Html>()
  9. }
  10. </div>
  11. };

Performance increases

We have Keyed list example that lets you test the performance improvements, but here is rough example of testing:

  1. Go to Keyed list hosted demo
  2. Add 500 elements.
  3. Disable keys.
  4. Reverse the list.
  5. Look at “The last rendering took Xms” (At the time of writing this it was ~60ms)
  6. Enable keys.
  7. Reverse the list.
  8. Look at “The last rendering took Xms” (At the time of writing this it was ~30ms)

So just at the time of writing this, for 500 components its a x2 increase of speed.

Detailed explanation

Usually you just need a key on every list item when you iterate and the order of data can change. So lets say you iterate through ["bob","sam","rob"] ended up with html:

  1. <div id="bob">My name is Bob</div>
  2. <div id="sam">My name is Sam</div>
  3. <div id="rob">My name is rob</div>

Then if your list changed to ["bob","rob"], yew would delete from previous html element with id=”rob” and update id=”sam” to be id=”rob”

Now if you had added a key to each element, html would stay the same, but in case where it changed to ["bob","rob"], yew would just delete the second html element since it knows which one it is.

Keys also help for weird cases where yew reuses html elements.

If you ever encounter a bug/“feature” where you switch from one component to another but both have a div as the highest rendered element. Yew reuses the rendered html div in those cases as an optimization. If you need that div to be recreated instead of reused, then you can add different keys and they wont be reused

Further reading