Set selected option in loop
Unanswered
Japanese Bobtail posted this in #help-forum
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('&', '&')}
</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('&', '&')}
</option>
))}
</select>