Next.js Discord

Discord Forum

Set selected option in loop

Unanswered
Japanese Bobtail posted this in #help-forum
Open in Discord
Japanese BobtailOP
Hello there. How can i set the html "selected" attribute in a nextjs loop to prevent the notice "Use the defaultValue or value props on <select> instead of setting selected on <option>."?

<select onChange={(e) => handleChange(e)}> {categories?.map?.(({ name, slug, id }) => ( <option key={id} value={slug} selected={currentCategory === slug}> {name.replace('&amp;', '&')} </option> ))} </select>

1 Reply

Japanese BobtailOP
I've solved this with:

<select onChange={(e) => handleChange(e)} value={currentCategory}> {categories?.map?.(({ name, slug, id }) => ( <option key={id} value={slug}> {name.replace('&amp;', '&')} </option> ))} </select>