简体   繁体   English

反应路由器使用历史。 History.push 更改 url 但不加载组件

[英]React Router useHistory. History.push changes url but does not load component

I want to have a simple button that when clicked redirects a user to a route defined in my Index.tsx file.我想要一个简单的按钮,单击该按钮时会将用户重定向到我的 Index.tsx 文件中定义的路由。

When the button is clicked, the url bar is correctly changed to "/dashboard", however my component (just an h1) does not appear.单击按钮时,url 栏正确更改为“/dashboard”,但是我的组件(只是一个 h1)没有出现。 If I reload chrome at that point it will appear.如果我在那时重新加载 chrome,它将出现。

Here's my code (Index.tsx):这是我的代码(Index.tsx):

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { Route, Switch } from "react-router-dom";
import { Router } from "react-router";
import { createBrowserHistory } from "history";
import Dashboard from "./components/Dashboard";

const appHistory = createBrowserHistory();

ReactDOM.render(
  <React.StrictMode>
    <Router history={appHistory}>
      <Switch>
        <Route exact path="/" component={App} />
        <Route path="/dashboard" component={Dashboard} />
      </Switch>
    </Router>
  </React.StrictMode>,
   document.getElementById("root")
  );

Here's my start of a Login form (the first route above, App, renders it).这是我登录表单的开始(上面的第一条路线,App,呈现它)。 (Login.tsx): (登录.tsx):

 import React, { useState } from "react";
 import {Button} from "@material-ui/core";
 import { useHistory} from "react-router-dom";

 export const Login: React.FC = (props) => {
     const classes = useStyles();
     let history = useHistory();

     const [email, setEmail] = useState<string>("");
     const [password, setPassword] = useState<string>("");

     const validateForm = (): boolean => {
        return true;
     };

     const handleLoginClick = (
       event: React.MouseEvent<HTMLButtonElement, MouseEvent>
      ) => {
         history.push("/dashboard");
     };

     return (
        <form>
         <div>
           <Button
            onClick={handleLoginClick}
            color="inherit"
            type="button"
           >
            Login
           </Button>
         </div>
        </form>
       );
 };

 export default Login;

Replace this line替换这一行

import { Router } from "react-router"

with

import {BrowserRouter as Router } from "react-router-dom";

Try importing the Router from react-router-dom - the rest seems correct尝试从react-router-dom导入Router - 其余的似乎是正确的

import { Route, Router, Switch } from "react-router-dom";

react-router is mostly for internal usage - you only interact with react-router-dom react-router主要用于内部使用 - 您只与react-router-dom交互

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应路由器问题:为什么 history.push 更改 url 但不更新组件 - React Router Question: why history.push changes url but doesn't update component React history.push 正在更新 url 但无法加载所需的组件 - React history.push is updating url but is not unable to load the desired component history.push 更改 url 但不呈现表单反应挂钩 - history.push changes url but does not render form react hooks React Router Dom & Firebase Auth: history.push 更改 url 但在 firebase auth 创建帐户后调用时不呈现新组件 - React Router Dom & Firebase Auth: history.push changes url but does not render new component when called after firebase auth create account React:通过公共组件中的 history.push 跟踪历史更改 - React: tracking history changes by history.push in common component React Router v5:history.push()改变地址栏,但不改变页面 - React Router v5: history.push() changes the address bar, but does not change the page history.push() 和自定义 URL 参数使用 react-router - history.push() and Custom URL parameters using react-router history.push() 更新 url 但不渲染组件 - history.push() updates url but does not render the component history.push() 重定向 url 但组件不呈现(reactjs 应用程序) - history.push() redirects the url but the component does not render (reactjs application) 在无限循环中反应路由器 history.push - React Router history.push in infinite loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM