79444322

Date: 2025-02-17 03:15:12
Score: 3.5
Natty:
Report link

It seems to be a bug with React 19. I have a similar problem, and I also tried multiple solutions such as setting the defaultValue for the <select></select> tag, or conditionally checking the selected value for the <option></option> tag to no avail.

You might as well try a more "manual" solution, which is to manipulate the DOM after the form state updates:

function createSpecialtyAction(prevState, formData) {
  //...your code
  const selectedValue = formData.get("schoolId");
  const selectEl = document.querySelector("select#schoolId");
  setTimeout(() => {
    if (selectEl) selectEl.value = selectValue;
  }, 100);
  return {...yourState};
}

This solution guarantees that you retain your state after submitting the form, but comes with the drawback of giving a bit of UI flickering.

Reasons:
  • Blacklisted phrase (1): I have a similar problem
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Me too answer (2.5): I have a similar problem
  • Low reputation (1):
Posted by: marcofleuranza