Skip to content

select value update fails when set to a newly added option simultaneously #4737

Open
@luisherranz

Description

@luisherranz
  • Check if updating to the latest Preact version resolves the issue

Describe the bug

Imagine you have a select element and its option elements in different Preact components, both subscribed to the same signal (or reading from the same context).

const Options = () =>
  items.value.map((item) => (
    <option key={item.id} value={item.id}>
      {item.id}
    </option>
  ));

const Select = ({ children }) => {
  const selectedItem = items.value.find((item) => item.selected).id;
  return <select value={selectedItem}>{children}</select>;
};

<Select>
  <Options />
</Select>

If you try to add a new option and immediately select it, Preact first updates the value attribute of the select element in the DOM before adding the new option, and therefore the change of the value attribute fails.

To Reproduce

Interestingly, the same code in React works fine: https://stackblitz.com/edit/vitejs-vite-m6klnuay

Steps to reproduce the behavior: just click on the button.

Expected behavior

After clicking the button, the select value should be the third, newly added option, but it's not. Actually, it is correct in the virtual DOM, but not in the DOM.

The same code works if you refactor it so that both elements are in a single element.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions