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

323
Views
React / NextJs, set select option state on change does not reflect

I am trying to do a simple router push in a select option with react, so far my router push works but I can't update my default value, I can't properly set my state, not sure what I am doing wrong here:

const navigation = [
    { name: 'Profil', href: '/unternehmen/profil' },
    { name: 'Geschichte', href: '/unternehmen/geschichte' },
]

const SubNavigation = () => {
    const [selectedSubNav, setSelectedSubNav] = useState('')
    const router = useRouter()
    const onSubNavChange = (e: ChangeEvent<HTMLSelectElement>) => {
        e.preventDefault()
        const subName = navigation.find((nav) => nav.href === e.target.value)?.name
        console.log('subName: ' + subName)
        setSelectedSubNav(subName)
        console.log('selectedSubNav: ' + selectedSubNav)
        router.push(e.target.value)
    }

    return (
        <div className="relative pb-5 sm:pb-0">
            <div className="mt-4">
                <div className="sm:hidden">
                    <select
                        id="current-tab"
                        name="current-tab"
                        defaultValue={selectedSubNav}
                        onChange={(e) => onSubNavChange(e)}
                    >
                        {navigation.map((navItem) => (
                            <option value={navItem.href} key={navItem.name}>
                                {navItem.name}
                            </option>
                        ))}
                    </select>
                </div>

The router push works, but I can't set the value in my select option to that value I am setting my state to.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

try update selectedSubNav with useEffect:

const navigation = [
    { name: 'Profil', href: '/unternehmen/profil' },
    { name: 'Geschichte', href: '/unternehmen/geschichte' },
]

const SubNavigation = () => {
    const [selectedSubNav, setSelectedSubNav] = useState('')
    const router = useRouter()

    useEffect(() => {
       const currentPath = navigation.find((nav) => nav.href === router.asPath)?.href
       setSelectedSubNav(currentPath)
    },[])

    const onSubNavChange = (e: ChangeEvent<HTMLSelectElement>) => {
        e.preventDefault()
        const subName = navigation.find((nav) => nav.href === e.target.value)?.name
        console.log('subName: ' + subName)
        setSelectedSubNav(subName)
        console.log('selectedSubNav: ' + selectedSubNav)
        router.push(e.target.value)
    }

    return (
                    <select
                        id="current-tab"
                        name="current-tab"
                        defaultValue={selectedSubNav}
                        onChange={(e) => onSubNavChange(e)}
                    >
                        {navigation.map((navItem) => (
                            <option value={navItem.href} key={navItem.name}>
                                {navItem.name}
                            </option>
                        ))}
                    </select>
     )
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!