JavaScript React Router Dom (4) Programmatically Navigate react router programmatically change route,react router programmatically change route

I am working on a React app that is 'remote controlled' and am trying to connect the app's tab navigation via Pusher. I am using react-router-dom and have set up my connection to Pusher and the channels it listens to in the componentDidMount. I need to have the app change URL navigation each time a message is returned but cannot figure out what the correct way to 'push' it is. I have google many threads about programmatically navigating react-router-dom and have tried this.history.push(), this.props.history.push() and it's always throwing an error that history is undefined. The listeners reside in the same Component as the routing itself.

    import React, { Component } from 'react';
    import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
    import axios from 'axios';
    import Pusher from 'pusher-js';

    import Component1 from './Component1';
    import Component2 from './Component2';

    export default class AppRouter extends Component{

    componentDidMount() {
          const pusher = new Pusher('8675309', { cluster: 'us2', encrypted: true });
          const nav_channel = pusher.subscribe('nav');
          nav_channel.bind('message', data => { this.props.history.push(data.message) });
    }

...

render(){
    return(
      <Router>
        <Switch>
          <Route path='/path1' render={() => <Component1 navigate={this.handleNavChange} />} />
          <Route path="/path2" render={() => <Component2 navigate={this.handleNavChange} />} />

        </Switch>
      </Router>
    )
  }

    }

I need the app to change the URL or the routing each time the message is received from another connected app but I cannot figure out how to make react-router-dom (v4) do it within the same component as the Router itself. Any information or pointing me to a resource would be highly appreciated.

Answer:1

You need to use the withRouter decorator from React-Router to get access to match, location, and history.

import { withRouter } from 'react-router'

Wrap your component when exporting

export default withRouter(yourComponent)

Then you will have access to history from props.

https://reacttraining.com/react-router/core/api/withRouter

Answer:2

I have a temperature scale (green up to some extent, then yellow and then red) in the background and need a temperature "bar" of sorts over it which: Refreshes after a fixed interval of time, in the ...

I have a temperature scale (green up to some extent, then yellow and then red) in the background and need a temperature "bar" of sorts over it which: Refreshes after a fixed interval of time, in the ...

  1. make div change width
  2. make padding not change width

When writing a gallery faced with a problem. There is pagination for the gallery. Each page should be different picture. As with the aid of AJAX upload new content to the gallery when switching pages? ...

When writing a gallery faced with a problem. There is pagination for the gallery. Each page should be different picture. As with the aid of AJAX upload new content to the gallery when switching pages? ...

  1. load content with ajax
  2. load content with javascript
  3. load content with jquery
  4. load content with ajax wordpress
  5. load content without page refresh
  6. load content with ajax jquery
  7. load content with scroll
  8. load content from url jquery
  9. load content in iframe
  10. load content in div jquery
  11. load content from another page javascript
  12. load content in div
  13. load content from url javascript
  14. load content in iframe jquery
  15. load content in div ajax
  16. load content from url jquery ajax
  17. load content from file php
  18. load content from url php
  19. load content from file javascript
  20. load content from file c#

I want an object like var obj = { "ABC" : { name: true, dob: true}, "CDE" : { name: true, dob: true}, "EFG" : { name: true, dob: true}, "CBA" : { name: true, dob: true}, "XYZ" : { ...

I want an object like var obj = { "ABC" : { name: true, dob: true}, "CDE" : { name: true, dob: true}, "EFG" : { name: true, dob: true}, "CBA" : { name: true, dob: true}, "XYZ" : { ...

  1. create object from array javascript
  2. create object from class
  3. create object from interface typescript
  4. create object from json
  5. create object from string
  6. create object from type c#
  7. create object from class python
  8. create object from dict python
  9. create object from spreadsheet salesforce
  10. create object from object javascript
  11. create object from string python
  12. create object from map javascript
  13. create object from class javascript
  14. create object from array lodash
  15. create object from json string
  16. create object from xml c#
  17. create object from another object javascript
  18. create object from interface java
  19. create object from file java
  20. create object from json java

I need to set for a div a margin-left in JavaScript in such a way that its value if the minimum between two lengths in different units, e.g. min(10pt,15vw). There is Math.min() that we can use to ...

I need to set for a div a margin-left in JavaScript in such a way that its value if the minimum between two lengths in different units, e.g. min(10pt,15vw). There is Math.min() that we can use to ...