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

210
Vistas
How to use if else condition with variable in Reactjs

I am working in reactjs,Right now i am getting current "url/hostname",Now i want to use this url with if else condtion,means i just want if url="/"(home page) then first header should display otherwise second word should display,In other words i want to know how we can use if else condition with dynamic variable ? Here is my current code

import { useRouter } from 'next/router'
const Header = () => {
const router = useRouter();
const url =router.asPath;

}
return (
    <>
    <div>
        //need to use if else condition based on "url" variable 
    </div>
   </>
    )
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use ternary operators there to render based on a condition, like this:

import { useRouter } from 'next/router'
const Header = () => {
const router = useRouter();
const url = router.asPath;

return (
    <>
    <div>
        {url === "something" ? <Foo /> : <Bar />}
    </div>
   </>
    )
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

strong text

import { useRouter } from 'next/router'
const Header = () => {
const router = useRouter();
const url = router.asPath;

return (
    <>
    <div>
        {url === "smth" ? <First /> : <Second />}
    </div>
   </>
    )
}

return (
    <>
    <div>
        {url === "smth" && <First />}
        {url === "smth" && <Second />}
    </div>
   </>
    )
}
you use both methods

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