reactjs - react component props does not have match
问题描述
I'm using react-routing-dom V5 in my react project. I need to pass props from App component (where I import Router) to child component through protected route. The problem is that props is empty, does not have match, location, history..
**App Component
import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
class App extends Component {
render(){
console.log(this.props); //props does not have match propreties
return (
<div className="App">
<Router>
<React.Fragment>
<Switch>
<ProtectedRoute path='/exemple' component={Exemple}/>
</Switch>
</React.Fragment>
</Router>
);
}
}
NOTE: I pass props from App to child component where i need to use this.props.match, but match is undefinded
解决方案
match
props is only available to components of a route:
<Route exact path="/foo" component={({ match }) => console.log(match)} />
If you need it on App, you need to create a route for App.
推荐阅读
- kubernetes - 尝试更新 k8s CR 资源时“请求实体太大:限制为 3145728”
- android - 如何在不使用 Gradle 命令的情况下使用 shell 脚本启动 leakcanary
- python - 使用 keras 模型进行预测时的代数错误 (model.predict())
- java - 查找相同值的 4 个连续数字 - 水平
- python - Python 中的 Choropleth 地图
- python - 情节未显示在 if 语句中
- swift - 在 xcode 中使用具有不同构建环境的 cocopods
- regex - 正则表达式至少包含一个大写字母且不包含至少一个下划线
- amazon-web-services - 使用 Terraform 使用 AWS Systems Manager Automation 进行集中式多账户修补
- java - 在 Clojure 中初始化 Java File 对象总是给出 Windows 中的相对路径。我如何给它绝对路径?