Skip to main content
Removed incorrect npm flag and improved a confusing sentence to make sense.
Source Link

npm i -S react-router-dom

If you are using redux or mobx as your state management library in your application, you may have come across issues with components that should be location-aware thatbut are not re-rendered after triggering an URL update

npm i -S react-router-dom

If you are using redux or mobx as your state management library in your application, you may have come across issues with components location-aware that are not re-rendered after triggering an URL update

npm i react-router-dom

If you are using redux or mobx as your state management library in your application, you may have come across issues with components that should be location-aware but are not re-rendered after triggering an URL update
"shape().isRequired" will return undefined. The isRequired is not needed for shape()
Source Link
import { withRouter } from 'react-router-dom'

const ButtonToNavigate = ({ history }) => (
  <button
    type="button"
    onClick={() => history.push('/my-new-location')}
  >
    Navigate
  </button>
);


ButtonToNavigate.propTypes = {
  history: React.PropTypes.shape({
    push: React.PropTypes.func.isRequired,
  }).isRequired,
};

export default withRouter(ButtonToNavigate);
import { withRouter } from 'react-router-dom'

const ButtonToNavigate = ({ history }) => (
  <button
    type="button"
    onClick={() => history.push('/my-new-location')}
  >
    Navigate
  </button>
);


ButtonToNavigate.propTypes = {
  history: React.PropTypes.shape({
    push: React.PropTypes.func.isRequired,
  }).isRequired,
};

export default withRouter(ButtonToNavigate);
import { withRouter } from 'react-router-dom'

const ButtonToNavigate = ({ history }) => (
  <button
    type="button"
    onClick={() => history.push('/my-new-location')}
  >
    Navigate
  </button>
);


ButtonToNavigate.propTypes = {
  history: React.PropTypes.shape({
    push: React.PropTypes.func.isRequired,
  }),
};

export default withRouter(ButtonToNavigate);
Correct order of original name and alias in import statement.
Source Link
import { RouterBrowserRouter as BrowserRouterRouter } from 'react-router-dom'

const ButtonToNavigate = ({ title, history }) => (
  <button
    type="button"
    onClick={() => history.push('/my-new-location')}
  >
    {title}
  </button>
);

const SomeComponent = () => (
  <Route path="/" render={(props) => <ButtonToNavigate {...props} title="Navigate elsewhere" />} />
)    

const App = () => (
  <Router>
    <SomeComponent /> // Notice how in v4 we can have any other component interleaved
    <AnotherComponent />
  </Router>
);
import { Router as BrowserRouter } from 'react-router-dom'

const ButtonToNavigate = ({ title, history }) => (
  <button
    type="button"
    onClick={() => history.push('/my-new-location')}
  >
    {title}
  </button>
);

const SomeComponent = () => (
  <Route path="/" render={(props) => <ButtonToNavigate {...props} title="Navigate elsewhere" />} />
)    

const App = () => (
  <Router>
    <SomeComponent /> // Notice how in v4 we can have any other component interleaved
    <AnotherComponent />
  </Router>
);
import { BrowserRouter as Router } from 'react-router-dom'

const ButtonToNavigate = ({ title, history }) => (
  <button
    type="button"
    onClick={() => history.push('/my-new-location')}
  >
    {title}
  </button>
);

const SomeComponent = () => (
  <Route path="/" render={(props) => <ButtonToNavigate {...props} title="Navigate elsewhere" />} />
)    

const App = () => (
  <Router>
    <SomeComponent /> // Notice how in v4 we can have any other component interleaved
    <AnotherComponent />
  </Router>
);
Fixing example number 4. Providing a better example for option number 1
Source Link
rgommezz
  • 14k
  • 2
  • 20
  • 23
Loading
Updating library API, the original answer was based on the alpha.4 version and with the official release, the data contract changed a bit. Incorporating Redirect as another option to achieve the goal; deleted 31 characters in body
Source Link
rgommezz
  • 14k
  • 2
  • 20
  • 23
Loading
Removed reference to alpha versions, now v4 is the latest stable. Provide workaround for common issue when using React Router with libraries like Redux and Mobx
Source Link
rgommezz
  • 14k
  • 2
  • 20
  • 23
Loading
added 1979 characters in body
Source Link
rgommezz
  • 14k
  • 2
  • 20
  • 23
Loading
added 1166 characters in body
Source Link
rgommezz
  • 14k
  • 2
  • 20
  • 23
Loading
Providing a bit more of background, plus more accurate examples
Source Link
rgommezz
  • 14k
  • 2
  • 20
  • 23
Loading
added 8 characters in body
Source Link
rgommezz
  • 14k
  • 2
  • 20
  • 23
Loading
Source Link
rgommezz
  • 14k
  • 2
  • 20
  • 23
Loading