reactjs - 使用受保护的路由时反应 js this.props.match.params 为空
问题描述
我有一个使用 react-routerv5 的 reacj js 应用程序。不幸的是,我无法得到this.props.match.params
它是空的:
Object { path: "/", url: "/", params: {}, isExact: false }
我的 App.js 如下
import React from "react";
//import logo from './logo.svg';
//import './App.css';
import {
Home,
Login,
Register,
NOTFOUND,
Dashboard,
QuestionList,
} from "./components";
import { routeNames } from "./routingParams";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import Proute from "./Proute";
function App() {
return (
<div className="App">
<Router>
<Switch>
<Route path={routeNames.HOME} exact component={() => <Home />} />
<Route path={routeNames.LOGIN} exact component={() => <Login />} />
<Route
path={routeNames.SIGNUP}
exact
component={() => <Register />}
/>
<Proute
path={routeNames.DASHBOARD}
exact
component={() => <Dashboard />}
/>
<Proute path={"/questions/:test"} component={QuestionList} />
<Route component={NOTFOUND} />
</Switch>
</Router>
</div>
);
}
export default App;
我的 Proute.js:
import React, { Component } from "react";
import { Redirect } from "react-router-dom";
import { AuthService } from "./services";
import { routeNames } from "./routingParams";
import { withRouter } from "react-router";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
class Proute extends Component {
state = {
isAuthenticated: false,
loading: true,
};
componentDidMount() {
var auth = new AuthService();
var _this = this;
auth.isAuthenticated().then(function (result) {
_this.setState({ isAuthenticated: result, loading: false });
});
}
render() {
const Component = this.props.component;
const { isAuthenticated, loading } = this.state;
if (loading) {
return <div>Loading</div>;
}
if (!isAuthenticated) {
return <Redirect to={routeNames.LOGIN} />;
}
return (
<Route>
<Component />
</Route>
);
//const isAuthenticated = true;
}
}
export default Proute;
还有我的问题清单:
import React, { Component } from "react";
import { useParams } from "react-router-dom";
import { withRouter } from "react-router";
class QuestionList extends Component {
// Question List page
constructor(props) {
super(props);
}
componentDidMount() {
const id = this.props.match.params.id;
console.log(id);
console.log(this.props.match);
}
render() {
document.title = "Question Lists";
return <div>Question</div>;
}
}
export default withRouter(QuestionList);
在我的 APP.js 中而不是使用Proute
我使用<Route .....
组件 am ABLE 来获取参数。但如果我使用 PRoute 则不会。我需要 Proute,因为我想执行受保护的规则,但是这样我没有得到params
.
解决方案
您应该将道具传递给组件
编辑:通过所有剩余的道具,而不是仅仅匹配
在 Proute.js 中:
render() {
const { component: Component, ...rest } = this.props;
const { isAuthenticated, loading } = this.state;
if (loading) {
return <div>Loading</div>;
}
if (!isAuthenticated) {
return <Redirect to={routeNames.LOGIN} />;
}
return (
<Route>
<Component ...rest />
</Route>
);
}
推荐阅读
- spring - Spring 查询注解
- django - 如何在django中实现拖放上传文件,而我已经实现了简单的文件上传
- react-native - 如何处理嵌套在View中的scrollView中的TextInput反应原生
- php - Facebook Graph API(将 v2.10 升级到 v3.10)
- maven - Maven spring-boot ueber-jar 和 lib
- google-drive-api - Flutter Google Drive 集成
- sql - 指定列的 Spark sql 问题
- android - Android - 错误:无法弄清楚如何将此字段保存到数据库中。你可以考虑为它添加一个类型转换器吗?
- c++ - C - 程序打印不正确
- regex - Graylog 搜索包含字符串