I have a react app where the react-router is initialized in the App.js. When the url is localhost:3000/id
the Home component is been rendered to the App.js, but i need to get the id
variable from the App.js to run the initBusinessDetails()
how can I access the id
variable from the App.js.
App.js code
import React from 'react'
import axios from 'axios';
import ReactLoading from 'react-loading';
import { Route, Link, BrowserRouter as Router } from 'react-router-dom'
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { getBusiness } from './actions/GetBusinessDetails';
import Home from './components/Home';
import Categories from './components/Categories';
class App extends React.Component {
initBusinessDetails(){
console.log(this.props)
const { params } = this.props.match
axios.get(`http://localhost:8081/getBusiness?businessId=`+params.id)
.then(res => {
this.props.getBusiness(res.data)
})
}
componentDidMount(){
this.initBusinessDetails();
}
render() {
return (
<div>
{!this.props.business ?
<div>
<ReactLoading type="bars" color="#000"/>
</div>:
<div>
<Router>
<Route exact path="/:id" component={Home}/>
<Route exact path="/:id/categories" component={Categories}/>
</Router>
</div>
}
</div>
)
}
}
function mapStateToProps(state){
return{
business:state.business
}
}
function matchDispatchToProps(dispatch){
return bindActionCreators({getBusiness : getBusiness},dispatch)
}
export default connect(mapStateToProps,matchDispatchToProps)(App)