为什么history.push(<路由器名称>)无法导航?

时间:2019-06-07 09:22:48

标签: javascript reactjs browser-history react-router-dom

我正在建立一个登录页面。如果登录成功,我要重定向用户的地方,否则应重定向到登录页面。

注意:不使用redux。

我试图通过“ react-router-dom”创建路由器历史记录,当我在搜索栏中按下新的端点时,路由器会更新,但它不会改变屏幕。我还尝试通过“ window.location.reload()”来更改屏幕,但是问题是我将如何将用户数据发送到下一个屏幕。

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import * as serviceWorker from './serviceWorker';
import { Route, Link, BrowserRouter as Router } from 'react-router-dom'
import Login from "./components/Login"
import signUp from "./components/signUp"
import Dashboard from "./components/dashboard"
import Recordings from "./components/Recordings"
import RauteHistory from './components/routeHistory';
import callDetails from './components/callDetails'
const routing = (
  <Router history={RauteHistory}>
    <div>
      <Route exact path="/" component={Login} />
            <Route path="/login" component={Login} />
      <Route path="/signup" component={signUp} />
            <Route path="/dashboard" component={Dashboard} />
            <Route path="/recordings" component={Recordings} />
            <Route path="/calldetails" component={callDetails} />
    </div>
  </Router>
)

ReactDOM.render(routing, document.getElementById('root'));


const loginButtonClick = () => {
    let userId, password
    userId = document.getElementById("userId").value;
    password = document.getElementById("password").value;
  loginService(userId, password)
        .then(responseJson => {
            if (responseJson.user_id){
                RauteHistory.push('/dashboard')
                window.location.reload()
            }
            else {
                handleClick()
            }
        })
        .catch(error => {
            console.log(error.error);
        });
}

0 个答案:

没有答案