Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

77
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda