Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-router v4 this.props.history.push(...) not working

I'm trying to route programatically using this.props.history.push(..) but it doesn't seem to work.

Here's the router:

import {  BrowserRouter as Router,  Route } from 'react-router-dom';  <Router>  <Route path="/customers/" exact component={CustomersList} />  <Route path="/customers/:id" exact component="{Customer} /> </Router> 

In CustomerList, a list of customers is rendered. Clicking on a customer (li) should make the application route to Customer:

import { withRouter } from 'react-router'  class Customers extends Component {   static propTypes = {     history: PropTypes.object.isRequired   }   handleCustomerClick(customer) {    this.props.history.push(`/customers/${customer.id}`);  }   render() {    return(     <ul>       { this.props.customers.map((c) =>         <li onClick={() => this.handleCustomerClick(c)} key={c.id}>           {c.name}         </li>      </ul>   )   } }  //connect to redux to get customers  CustomersList = withRouter(CustomersList); export default CustomersList; 

The code is partial but illustrates perfectly the situation. What happens is that the browser's address bar changes accordingly to history.push(..), but the view does not update, Customer component is not rendered and CustomersList is still there. Any ideas?

like image 357
Andrea Ongaro Avatar asked Jun 01 '17 16:06

Andrea Ongaro


2 Answers

So I came to this question hoping for an answer but to no avail. I have used

const { history } = this.props; history.push("/thePath") 

In the same project and it worked as expected. Upon further experimentation and some comparing and contrasting, I realized that this code will not run if it is called within the nested component. Therefore only the rendered page component can call this function for it to work properly.

Find Working Sandbox here

  • history: v4.7.2
  • react: v16.0.0
  • react-dom: v16.0.0
  • react-router-dom: v4.2.2
like image 148
Delanyo Aborchie Avatar answered Sep 17 '22 13:09

Delanyo Aborchie


It seems things have changed around a bit in the latest version of react router. You can now access history via the context. this.context.history.push('/path')

Also see the replies to the this github issue: https://github.com/ReactTraining/react-router/issues/4059

like image 45
cgat Avatar answered Sep 21 '22 13:09

cgat