Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!