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

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

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 Relatório

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