javascript - 反应路由器 this.props.history.location 未定义
问题描述
我对反应和反应路由器很陌生,我遇到了一些问题。我想实现一个重定向功能可以这么说,但它给了我this.props.history.location is undefined
错误
index.js - 应用在这里被路由器包裹
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './app/App.js';
import Test from "./pages/users/users";
import {BrowserRouter, Redirect, Route, Router, Switch} from "react-router-dom";
import createHistory from "history/createBrowserHistory"
import registerServiceWorker from './registerServiceWorker';
import history from './utilities/history'
const app = (
<Router history={history}>
<Route path="/:currentPage?" component={App} />
</Router>
);
ReactDOM.render(app, document.getElementById('root'));
registerServiceWorker();
App.js -- 主应用
export class App extends Component {
state = {};
handleChange = () => {
console.log(this.props);
const {user, match, history} = this.props;
const currentPath = match.params.currentPage;
const userIsSigningIn = currentPath === "/" + SIGN_IN_PAGE.path;
console.log(this.state.user);
if (this.state.user && currentPath === "/sign-in") {
// history was passed from client/src/index.js
this.props.history.replace('/remittances');
} else if (currentPath !== "/sign-in" && !this.state.user) {
this.props.history.replace('/sign-in');
}
return;
render() {
//this is our initial page
const {user, match, history} = this.props;
const currentPath = match.params.currentPage;
return (
<div className="page-container">
<Switch>
{/*note: important to pass functions as lambdas */}
<Route path="/sign-in" render={() => <SignInPage attemptSignIn={this.attemptSignIn}/>}/>
<Route path="/inventory" render={() => <InventoryPage/>}/>
<Route path="/remittances" render={() => <RemittancePage/>}/>
<Route path="/inventory" render={() => <RemittancePage/>}/>
<Route path="/users" render={() => <UsersPage/>}/>
</Switch>
{/*render navbar if there is a user and path is not sign-in*/}
{!this.state.user && currentPath !== "/sign-in" &&
<NavBar/>
}
</div>
);
}
}
export default withRouter(App)
任何形式的帮助都会谢谢你!
解决方案
推荐阅读
- html - 浏览器调整大小时将子 div 文本换行到下一行
- javascript - pdf阅读器中的放大/缩小画布,导致鼠标坐标改变它的位置
- java - ListView 仅显示 ListView 第一个位置的最后一项
- javascript - JSON Stringify 忽略 Redis 发布上的嵌套对象
- php - 如何解决错误编号:1048 列“photo_pict”不能为空?
- jquery - 我们可以在 jquery html 函数中使用 angularJS 字符串插值({{}})吗
- r - 针织:错误:pandoc 文档转换失败,错误 61
- php - Laravel:在 PDO 对象上指定了一个不受支持的属性。Ubuntu 上的 MSSQL
- bash - 无法使用期望语句将数据添加到文件 Tcl
- php - 如何在多维数组中找到一个键?