yew: Regression: elements are re-rendered even if they didn't change, when there's a list below them (worked in v0.19.3)

Problem Elements are re-rendered even if they didn’t change, when there’s a list below them. It works correctly in yew 0.19.3, but no longer in yew 0.20.0.

This is especially problematic when the re-rendered element is e.g. a <video> since when it gets re-rendered it stops and resets to the beginning. This is currently blocking me from updating 0.19.3 -> 0.20.0

Steps To Reproduce

  1. Have a component that renders an element and a dynamic list below it
  2. Change the list to cause a re-render of the component
  3. The elements coming from the list get rendered, but so does the other element too (this can be easily seen in the chrome devtools since you can see the element flash when it changes)

Run/see the full repro in this repo: https://github.com/andoalon/yew-bug (just run trunk serve --open)

In short, it boils down to this:

#[function_component(Video)]
pub fn video() -> Html {
    let number = use_state_eq(|| None::<i64>);

    let time_update = {
        let number = number.clone();
        Callback::from(move |event| {
            // number_from_video() returns some number
            // based on the video's current position
            number.set(number_from_video(event));
        })
    };

    let number_vec = {
        if let Some(num) = *number {
            vec![html! {
                <button class="my-button">
                    {format!("Number: {num}")}
                </button>
            }]
        } else {
            Vec::new()
        }
    };

    let number_opt = number.map(|num| html! {
        <button class="my-button">
            {format!("Number: {num}")}
        </button>
    });

    html! {
        <>
            // The video element gets re-rendered when the list below changes
            //  in cases 2, 2.1, 3 and 3.1 (it does not in case 1). It also doesn't get
            // re-rendered in yew 0.19.3
            <video muted=true controls=true type={VIDEO_TYPE} src={VIDEO_URL}
                ontimeupdate={time_update}>
            </video>

            // Case 1: Works
            // if let Some(num) = *number {
            //     <button class="my-button">
            //         {format!("Number: {num}")}
            //     </button>
            // }

            { number_vec } // Case 2: Bug
            //{ for number_vec } // Case 2.1: Bug

            //{ number_opt } // Case 3: Bug
            //{ for number_opt } // Case 3.1: Bug
        </>
    }
}

Expected behavior Elements are only re-rendered if they changed.

Observed behavior with <video> It resets to the beginning and stops when the list below it changes. On a different note, I noticed that the muted attribute of <video> seems to not be working. Not sure if I misused it 🤔

Environment:

  • Yew version: v0.20.0 (previously was using v0.19.3 where the issue was not present)
  • Rust version: 1.69.0 (stable)
  • Target, if relevant: wasm32-unknown-unknown (not sure if relevant)
  • Build tool, if relevant: trunk (not sure if relevant)
  • OS, if relevant: Windows 10 (not sure if relevant)
  • Browser and version, if relevant: reproduced in Chrome 112.0.5615.138 (Official Build) (64-bit) and Firefox 112.0.2 (64-bit)

Questionnaire

  • I’m interested in fixing this myself but don’t know where to start
  • I would like to fix and I have a solution
  • I don’t have time to fix this right now, but maybe later

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 17 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Sorry about the delay. I finally have some time to sit down and explain my opinion on this issue.

In short, I do not think expanding {for expr} into the parent VList is a desirable behaviour. In my opinion, all html! declared elements, components and expressions ({for expr} itself should be keyless-safe, but items expanded by expr isn’t.) should be keyless-safe, which includes this issue.

My main concern of the current {for expr} behaviour, is that this can render previously keyless-safe VList / Children key-required, by simply inserting {for expr} into somewhere of an html!, leading to confusing, hard to debug scenarios.

However, this is not the only concern around this extending behaviour. So I will discuss this in detail:

1. An extending for-expression is hard to spot, and hard to maintain.

Expanding {for expr} into the parent VList, renders all adjacent elements defined by html! key-required.

Whilst we can make a distinction between {contents} and {for contents} I think due to limitations of Rust, we cannot make {contents} to implement for any type that is an iterator, the blank implementation is already implemented for std::fmt::Display. This leaves user with an iterator content with no choice but to use {for contents} as that is more intuitive (and efficient?) to do than .collect::<Vec<_>>().

The above, which increased the likelihood of users using {for contents}, combined with {for expr}'s side effect could lead users into tedious debugging scenarios.

Suppose that a developer has created the following page:

html! {
    <div class="page-container gutter-left gutter-right">
        <main class="main">
            <header class="header header-stand gutter-bottom flex-container">
                <div class="header-container">
                    <h1 class="header-page-title h1-standout highlight gutter-bottom">{"Title"}</h1>
                    <nav>
                        <a href="/" class="nav-link current home nav-item-flex">
                            <span class="link-text">{"Home"}</span>
                        </a>
                        <a href="/" class="nav-link nav-item-flex">
                            <span class="link-text">{"Articles"}</span>
                        </a>
                        <a href="/" class="nav-link nav-item-flex">
                            <span class="link-text">{"Home"}</span>
                        </a>
                       <a href="/" class="nav-link nav-item-flex">
                            <span class="link-text">{"Log In"}</span>
                        </a>
                       <a href="/" class="nav-link nav-item-flex">
                            <span class="link-text">{"Register"}</span>
                        </a>
                    </nav>
                </div>
            </header>

            <div class="main-content max-width-xl">
                <div class="main-content-container gutter-top gutter-buttom">
                    {article_1}
                </div>
            </div>

            <div class="main-content max-width-xl">
                <div class="main-content-container gutter-top gutter-buttom">
                    {article_2}
                </div>
            </div>

            <div class="main-content max-width-xl">
                <div class="main-content-container gutter-top gutter-buttom">
                    {article_3}
                </div>
            </div>

            <div class="main-content max-width-xl">
                <div class="main-content-container gutter-top gutter-buttom">
                    {article_4}
                </div>
            </div>

            <div class="main-content max-width-xl">
                <div class="main-content-container gutter-top gutter-buttom">
                    {article_5}
                </div>
            </div>

            <form class="form-contact-us">
                <div class="form-field-container">
                    <input
                        name="email"
                        placeholder="E-Mail Address"
                        type="email"
                        class="content-field-input input-text-field gutter-top gutter-bottom styled-rounded"
                    />
                </div>
                <div class="form-field-container">
                    <input
                        name="phone"
                        placeholder="Phone Number"
                        type="text"
                        class="content-field-input input-text-field gutter-top gutter-bottom styled-rounded"
                    />
                </div>
                <div class="form-field-container">
                    <input
                        name="phone"
                        placeholder="Phone Number"
                        type="text"
                        class="content-field-input input-text-field gutter-top gutter-bottom styled-rounded"
                    />
                </div>
                <div class="form-field-container full-width">
                    <textarea
                        name="message"
                        placeholder="Message"
                        class="content-field-input input-textarea gutter-top gutter-bottom styled-rounded"
                    ></textarea>
                </div>
                {for random_3_questions}

                <div class="form-field-container">
                    <input
                        type="submit"
                        value="Submit"
                        class="content-field-input button gutter-top styled-rounded"
                    />
                </div>
            </form>

            {for seasonal_contents}

            <div class="footer-paginator">
                <div class="paginator-arrow paginator-prev">
                    <i class="arrow-left"></i>
                    <span>{"Previous Page"}</span>
                </div>
                {paginator_nos}
                <div class="paginator-arrow paginator-next">
                    <i class="arrow-right"></i>
                    <span>{"Next Page"}</span>
                </div>
            </div>

            <footer class="page-footer">
                <div class="footer-container">
                    <div class="footer-line">
                        <h5>
                            <span class="font-secondary color-secondary">{"©"}</span>
                            <span class="font-primary color-secondary">{"My Website"}</span>
                        </h5>
                    </div>
                </div>
            </footer>
        </main>
        <aside class="page-sidebar gutter-right styled-rounded">
            {sidebar_contents}
        </aside>
    </div>
}

By including {for seasonal_contents} and {for random_3_questions}, all child nodes of <main /> and <form /> are now key-required.

The developer of this page, is finding that the contents are not updating as expected that contents are getting resetted from time to time, and is trying to debug this page.

This creates some questions for the debugging process:

  1. How long would the developer take to realise that {for expr} is the issue here and it is rendering its siblings as key-required?
  2. Is it easy to figure out the elements that should be keyed and fix them?
  3. Would it be easy for the developers to keep track of elements when being placed under which element will require keys as they extend this page?
  4. Half a year later, the developer comes back to add some new features, what is the likelihood that they still remember Q3?

In addition, this is not only a concern of when one is working on their own code, but also when reviewing other people’s code when adding a {for expr} looks like a small change so the reviewer usually do not expand the code and think it very deeply before moving on to other parts of the PR.

2. What key should a VNode use?

If we assume that the first point isn’t an issue, this developer is willing to deal with it, expanding into the same VList still has another issue that can very easily forfeit the user’s effort of trying to key elements.

Let’s assume that we have a component named Parent:

#[function_component]
fn Parent(props: &ChildrenProps) -> Html {
    html! {
        // Let's assume that GrandParent
        // process Children by manipulation so we cannot use any `<></>` isolation tricks.
        <GrandParent>
            <header key="header">{"Parent Header"}</header>
            {for props.children.iter()}
        </GrandParent>
    }
}

The author knows that if an for-expression is used, they need to key all the sibling elements as well. So they keyed the header element. As it is fully keyed, Children of the Parent component should reconcile as expected, given we have a way to enforce Children to be fully keyed. The author then published the Parent component as a crate to crates.io.

A couple days later, a user created an App component using Parent. The user also knows that they have to key all elements if an for-expression is used so they keyed their app header.


#[function_component]
fn App() -> Html {
    let contents: Vec<Html> = create_contents();

    html! {
        <Parent>
            <div key="header">{"My App Header"}</div>
            {for contents}
        </Parent>
    }
}

Despite both sides have taken precaution to make sure that all items are keyed, the application still doesn’t work properly. The effort of both sides are nullified because they both used header as the key.

In this case, would it be reasonable to assume that it’s the responsibility of the author of Parent to exhaustively list the keys they are using and the creator of App should read the fine manual of the parent component?

3. It’s very unintuitive to key certain expressions.

If a VList not fully-keyed, the behaviour is equivalent to unkeyed. So it’s important to key every child node in a VList. By adding {for expr}, all siblings are required to be keyed.

However, certain expressions are very unintuitive to key. This is OK as currently as I would assume that they are never designed to be keyed (nor needed if used in html!).

For the following html!,


html! {
    <Parent>
        if show_header {
            <div>
                {"My App Header"}
            </div>
        }   
        {for contents}
    </Parent>
}

It might be easy to assume that it should be keyed as following:

html! {
    <Parent>
        if show_header {
            <div key="header">
                {"My App Header"}
            </div>
        }   
        {for contents}
    </Parent>
}

However, this does not cover the default value of this expression as it is VNode::default(), so it actually needs to be keyed in the following way by adding a fragment:

html! {
    <Parent>
        <key="header">
            if show_header {
                <div>
                    {"My App Header"}
                </div>
            }
        </>
        {for contents}
    </Parent>
}

The extreme (yet common) scenario would be that we need to key text nodes.

html! {
    <Parent>
        <key="header">
            {"My App Header"}
        </>
         <key="header2">
            {"My App Header 2"}
        </>
        {for contents}
    </Parent>
}

I think this is not only hard to justify, but also beyond the effort that most people is willing to spend on a feature.

4. Conclusion

In my opinion, keys should only be required in the following scenarios:

  1. Inside of html created by Vec / Iterator.

    Components’ keyless-safety should not be impacted by anything else declared in html!. (Or else we should make a unsafe {} clause for expressions that could impact it this way.)

  2. When a component needs to be forcibly replaced,

    i.e.: a different key for the same tag / component forces a destroy and create cycle, instead of reusing the existing component / tag.

I think the behaviour described by @dylanowen is also more desirable, which we should consider.

I personally think this limits scenarios where keys are required, makes key requirement easier to remember and is more difficult to get confused of when do you need to use it.

The reality is, even with React’s easier key requirement and warnings in developer console, it is still not uncommon to see React code that misuse / forget keys.

I certainly wish that we do not make the key requirement more complicated and subtle (1), making coming up keys itself more difficult (2), or make users life harder (3).

In addition, the only reason why {for expr} should be expanded to current VList I can come up with is because we want to manipulate Children in which itself doesn’t work properly for reasons I described in #3256.

I believe this has not been fixed.

{fox x} is still problamatic and needs a VList to help with that.

You need to force it, and jsx syntax might differ here.

A falsy value for a in React, renders nothing in the DOM, but still evaluate to false when you construct the React’s children. This always result in a slot being reserved even if the element is not rendered. When you try to force it to happen with arrays manually, React will emit a warning to the developer console stating that keys are required in these situations.

The JSX expression is more equivalent to:

if (a) {
    elements.push(<h2>Start editing to see some magic happen!</h2>);
  } else {
    elements.push(false);
  }

That’s why declarative syntax is safe without keys where arrays and iterators isn’t as in a JSX expression / HTML macro, the declared block can always indicate there is an expression regardless of whether it is rendered.