Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

83
Vistas
First Option of Select Element Not Working Unless Another Option Is Selected First
<select value={author} onChange={({ target }) => setAuthor(target.value)} >
          {authors.map(author => 
            <option key={author.name} value={author.name}>
              {author.name}
            </option>)}
        </select>

This is in React/NodeJS. Everything works as expected, and the first option/author.name is displayed by default. However, it is not being set as the default value for the React state, and clicking on the dropdown and then selecting it does not change this. If another option is selected and then you select the first option, it works as expected.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to set the state (author in this case) for the first time manually, because the onChange event only fires when you change the selected option, so it does not set the state when it loads. you have two option:

  1. If authors known before hand you can use this:

const [author,setAuthor] = useState(authors[0].name)

  1. If you load authors asynchronously you can wait for them to load and then set the author using useEffect:
useEffect(()=>{
if(authors && authors.length>0){
setAuthor(authors[0].name);
},[authors])
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda