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

178
Vistas
How to get main url of project in reactjs

I am working with reactjs and i want to get fullpath/hostname/baseurl of my project (inside component file),How can i do this ?I tried with following code,but not working,giving me following error

"Unexpected token `{`. Expected * for generator"

I tried with following code

import absoluteUrl from 'next-absolute-url';
class Header extends Component {
const { origin } = absoluteUrl(req)
const apiURL = ${origin}
render() 
    {
        ...//my code
    }   
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

How about using the window object.

Note: when using window, do wrap it inside the below if condition, else it will error out when you're using SSR.

import '../styles/globals.css';

function MyApp({ Component, pageProps }) {
  let fullPath = '';
  if (typeof window !== undefined) {
    fullPath = window.location.hostname;
  }
  console.log(fullPath);
  return <Component {...pageProps} className={fullPath === 'insert your path here' ? 'insert class to apply if true' : 'insert class to apply if false'}/>;
}

export default MyApp;

stackblitz

about 4 years ago · Juan Pablo Isaza Denunciar

0

So firstly, remember your code runs on client and server so any access to window needs to be wrapped in a check.

  if (typeof window !== 'undefined') {
    var path = location.protocol + '//' + location.host + '/someting'; // (or whatever)
  } else {
    // work out what you want to do server-side...
  }
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