[英]How to access component parameters from app.js in react router
I have a react app where the react-router is initialized in the App.js.我有一个反应应用程序,其中反应路由器在 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.当 url 为
localhost:3000/id
,Home 组件将呈现给 App.js,但我需要从 App.js 获取id
变量以运行initBusinessDetails()
如何从 App 访问id
变量.js。
App.js code App.js 代码
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)
Accessing Match Params访问匹配参数
A component being rendered under the component
prop from react-router-dom
also receives a match
prop.在来自
react-router-dom
的component
道具下渲染的component
也会收到match
道具。
You'll have access to match objects in various places:
您将可以访问各个位置的匹配对象:
- Route component as this.props.match
路由组件为 this.props.match
- Route render as ({ match }) => ()
路由渲染为 ({ match }) => ()
- Route children as ({ match }) => ()
将孩子路由为 ({ match }) => ()
- withRouter as this.props.match
withRouter 作为 this.props.match
- matchPath as the return value
matchPath 作为返回值
To access the id
from it:要从中访问
id
:
initBusinessDetails() {
const { params } = this.props.match;
axios.get(`http://localhost:8081/getBusiness?businessId=${params.id}`)
.then(res => {
this.props.getBusiness(res.data)
});
}
But this Code is Outside my Router但此代码在我的路由器之外
Now this code is outside of your router, so you need to render something inside your router that can call that function.现在这段代码在你的路由器之外,所以你需要在你的路由器内部渲染一些可以调用该函数的东西。 This is easy since you are still only using a
Router
so all matched routes are rendered ( As opposed to a Switch
which only returns the first matched route ).这很容易,因为您仍然只使用
Router
因此会呈现所有匹配的路由(与只返回第一个匹配路由的Switch
相反)。 Render another route with an inline functional component.使用内联功能组件渲染另一条路线。 Could do class-based as well, but would require a tweak in order to work.
也可以基于类,但需要进行调整才能工作。 Let me know if this is the case.
如果是这种情况,请告诉我。
<Route
exact
path="/:id"
component={({ match }) => {
// add any logic here to do anything when this component is
// mounted/rendered, like call `initBusinessDetails` with
// the `id` param
return null; // return nothing to the UI
}}
/>
Now, this will trigger that function whenever the router rerenders that route, so you may need to add logic out in App
and your axios call to ensure you only initialize once.现在,每当路由器重新渲染该路由时,这都会触发该功能,因此您可能需要在
App
和 axios 调用中添加逻辑输出以确保您只初始化一次。 If you can use hooks, then an useEffect
hook right in the inline functional component would do the trick.如果您可以使用钩子,那么内联函数组件中的
useEffect
钩子就可以解决问题。
initBusinessDetails(id) {
axios.get(`http://localhost:8081/getBusiness?businessId=${id}`)
.then(res => {
this.props.getBusiness(res.data)
});
}
...
<Route
exact
path="/:id"
component={({ match }) => {
useEffect(
() => initBusinessDetails(match.params.id),
[] // call only once when mounted
);
return null; // return nothing to the UI
}}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.