question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with programmatically changing routes

See original GitHub issue

First off, thanks for creating a clean and well-documented starter app. We are in the process of migrating our existing code to use the tools provided here.

We are running into an issue with React Router v4 when trying to navigate to another route programmatically via push. At first we tried history.push(...), but that gave us errors saying push was undefined.

After looking through the issues, we found a thread that’s relevant: #36. I think the issue was made before the full React Router v4 implementation was made to the boilerplate, but we went ahead with attempting the solution anyway. We added this to a connected component:

import { push } from 'react-router-redux'
...
const mapDispatchToProps = (dispatch) => {
  return {
    push: v => dispatch(push(v)),
    ...
  }
};

And tried changing routes via

this.props.push('/newroute')

Although the browser’s URL bar changes location, but the page does not update. Can anyone tell me what’s missing here?


Steps to reproduce:

  1. Replace ./src/containers/Home/index.js with the following:
/* eslint-disable react/sort-comp */
/* @flow */

import React, { PureComponent } from 'react';
import { connect } from 'react-redux';
import type { Connector } from 'react-redux';
import Helmet from 'react-helmet';
import { push } from 'react-router-redux';

import * as action from './action';
import type { Home as HomeType, Dispatch, Reducer } from '../../types';
import UserList from '../../components/UserList';
import styles from './styles.scss';

type Props = {
  home: HomeType,
  fetchUsersIfNeeded: () => void,
  push: () => void,
};

// Export this for unit testing more easily
export class Home extends PureComponent {
  props: Props;

  static defaultProps: {
    home: {
      readyStatus: 'USERS_INVALID',
      list: null,
    },
    fetchUsersIfNeeded: () => {},
  };

  componentDidMount() {
    this.props.fetchUsersIfNeeded();
  }

  renderUserList = () => {
    const { home } = this.props;

    if (!home.readyStatus || home.readyStatus === action.USERS_INVALID ||
      home.readyStatus === action.USERS_REQUESTING) {
      return <p>Loading...</p>;
    }

    if (home.readyStatus === action.USERS_FAILURE) {
      return <p>Oops, Failed to load list!</p>;
    }

    return <UserList list={home.list} />;
  }

  render() {
    return (
      <div className={styles.Home}>
        <Helmet title="Home" />
        {this.renderUserList()}
        <button onClick={() => this.props.push('/UserInfo/6')}>Click this button</button>
      </div>
    );
  }
}

const connector: Connector<{}, Props> = connect(
  ({ home }: Reducer) => ({ home }),
  (dispatch: Dispatch) => ({
    fetchUsersIfNeeded: () => dispatch(action.fetchUsersIfNeeded()),
    push: a => dispatch(push(a)),
  }),
);

export default connector(Home);
  1. Run the project and click the “Click this button”
  2. URL will change but the component won’t update

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
danyimcommented, Apr 16, 2017

Great. Thanks for fixing and maintaining this boilerplate!

1reaction
wellyshencommented, Apr 11, 2017

@danyim Thanks man, it really help me 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Trouble programmatically changing routes with React ...
I'm having a lot of trouble trying to navigate with React Router programmatically. I can change routes once, but I cannot change routes...
Read more >
RR V4 Programmatically Change Routes · Issue #3926 - GitHub
Attempt to access programmatically the ability to change routing. Expected Behavior. history.push (path) is what I would expect to push to a new ......
Read more >
Best way to programmatically change $routes? - Google Groups
So far, the only way I can think of to do this is to iterate through $route. routes and find the correct route,...
Read more >
Programmatically Navigate with React Router - Telerik
The effect of routing programmatically is on the same page as no route changing or, at other times, may bring about a need...
Read more >
Is it possible to add routes programmatically in SvelteKit?
For this to work I need to programmatically add routes to SvelteKit, bypassing the filesystem based routing. It should work with API routes, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found