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

153
Visualizações
How to make Navigation work with Routes if they are in the different packages?

I have some components in react App that I moved to a separate lerna package (apart from App.js and Routes.js) and it caused that my Navigation together with History stopped working). Any suggestion how to fix this separation that it work to Navigate to different URL?

Here are the files in that separate packages:

History.js:

 import { createBrowserHistory } from 'history';

 export default createBrowserHistory(); 

Navigation.js

 import history from '../utils/History';

  export default function Nav(loc, state) {
  history.push(loc, state);
  }

function in the class that I want t work with Nav import Nav from '../Navigation';

     class MyComponent extends Component {
     constructor(props) {
     super(props);
     this.handleClick = this.handleClick.bind(this);
     }

    handleClick(c) {  
    Nav('/main/' + c.name;)
    }

And here is Routes from another package(simplified)

  import { MyComponent } from '@MyComponents';
  import { Switch, Route, Redirect } from 'react-router-dom';

  const Routes = () => (
  <Switch>
  <Route
  exact
  path='/'
  render={props => (
      <MyComponent
        {...props}      
      />bla...bla..))

I get my URL in the browser http://localhost:3000/main/componentname But it doesn't redirect to the URL, only if I refresh

Any suggestions how I can make Navigation work if Routes is in one component and Navigation + History in another? Callback functions a bit difficult to implement if the Application becomes too complicated. Is there any other ways to fix it?

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

0

I think you don't need Navigation.js & History.js you just change your YourComponent with:

YourComponent

class MyComponent extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}

handleClick(c) {
  
this.props.history.push(`/main/${c.name}`)
}

To understand how to implement this, you need to know that when a component is rendered by the router, three properties are passed as parameters:

  1. match
  2. location
  3. history

in this case we use history in this.props.history to push our path into /main/${c.name}

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