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

124
Visualizações
Routing React Component based on hostname from the URL

I want to load React Component based on host name , Example , let say We have React application 'Frontend' and URL to access this app is <base_url>/test/login or <base_url>/test/home. Now I want to access this app from 2 different host name.

  1. test.abc.com/test/login , test.abc.com/test/home
  2. test.xyz.com/test/login , test.xyz.com/test/home

Now If user use #1 then it will show the login/home/logout page for test.abc.com only and for #2 login/home/logout page is different. Basically I need to customized the UI based o hostname.

Can anyone tell me , How can I achieve this scenario or is there any other way to do that?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

  1. Test the value of location.hostname
  2. Select your component based on that
  3. Pass it to your route

For example:

import { ABCLogin, XYZLogin } from "./LoginComponents";
const Login = location.hostname === "test.abc.com" ? ABCLogin : XYZLogin;

const App = () => {
    /* ... */
    <Route path="login" element={<Login />}>
    /* ... */
}

From a comment on another answer:

If the app is running from 100 hostnames and the login/home/logut and others are different based on host name , then in this case whether this approach is applicable too?

Going from 2 (as specified in the question) to 100 is something of a goalpost move, but…

Since including 100 different Login components would bloat the application, I would do the selection at build time. The specifics would depend on which toolchain you were using to build your production app.

For example, if you were using Webpack you could do something like:

resolve: {  
    alias: {
        components: path.resolve(__dirname, 'src/components/'),
        'components/Login': path.resolve(__dirname, `src/components/${process.env.TARGETHOSTNAME}Login`)
    }
}

This would require that you set the TARGETHOSTNAME environment variable before building.

about 4 years ago · Juan Pablo Isaza Relatório

0

Use window.location.hostname and useEffect to achieve desired behaviour. Basically check the url inside useEffect and redirect using ReactRouterDOM library or something similar.

about 4 years ago · Juan Pablo Isaza Relatório

0

get the hostname with the javascript function location.hostname; if the hostname is test.abc.com redirect to test.abc.com/logout else redirect to test.xyz.com/logout

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