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

151
Vistas
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 Respuestas
Responde la pregunta

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