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.
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
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 ...
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? ...
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" : { ...
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 ...