vercel / vercel/next-learn

Next.js a <select> with a list of <option> components resetting its value when the form is submitted

Open
#1,012 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
4.8k
Forks
2.2k
PR merge metrics
No merged PRs in 30d

Description

In chapter 14 (Improving Accessibility) there is a strange behavior for
select with a list of option components.

The select element's value is not persisting after submitting the form
even though the value of formData.customerId is persisting.

After click button "submit", the form rerenders and the console.log(formData.customerId) logs the correct value (a UUID represeting the customer) , but the select element still resets to "nothing".

Link to the code that reproduces this issue
https://nextjs.org/learn/dashboard-app.

To Reproduce

  1. Start the app by running pnpm run dev
  2. Open the app in the browser at http://localhost:3000/dashboard/invoices/create
  3. Choose a customer, fill in an amount and click the submit button
  4. Result: the Choose customer option go back to "Select a customer" and the amount is empty after submit. All input is lost.

Current vs. Expected behavior
I expect the form to contain the original data, since I am not resetting the form or redirecting. This is important in case of form validation errors (which I didn't include to keep the code simple).

Before submit:
Image

After submit:

Image

The issue has been raise at StackOverflow on this link https://stackoverflow.com/questions/79386275/next-js-controlled-select-element-resetting-its-value-when-the-form-is-submitt.

But no one has answered since then.

I have been search and try to find the solution but still no result.

Is there any solution on this ?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run pnpm run dev and reproduce the issue at /dashboard/invoices/create in the linked dashboard app. Inspect the create-invoice form and its submit behavior, comparing the selected customer and amount before and after submission. Done means the entered values remain visible after submission, including when validation errors occur.

Written by the indexing model from the issue text.

Assessment

Tech stack
next.js, typescript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.