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

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

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 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