简体   繁体   English

在无状态功能组件内反应路由器重定向

[英]React router Redirect inside a stateless functional component

I am new to react and developing an application which allows user to navigate across pages.我是反应和开发允许用户跨页面导航的应用程序的新手。

I created a TypeScript Project with following我创建了一个 TypeScript 项目,如下所示

App.tsx应用程序.tsx

import React from 'react';
import {BrowserRouter, Route, Switch} from 'react-router-dom';
import Login from './login';
import Welcome from "./welcome";
import About from './about';

const RedirectRoute = (props) => {
    // if (!authenticated) {

    // }
    return <Route {...props} />
}

const App: React.FC = () => {

    return (
        <BrowserRouter>
            <div>
                <Switch>
                    <RedirectRoute path="/" component={Welcome} exact/>
                    <RedirectRoute path="/login" component={Login} exact/>
                    <RedirectRoute path="/about" component={About} exact/>
                </Switch>
            </div>
        </BrowserRouter>
    );
};

export default App;

welcome.tsx欢迎.tsx

import React from 'react';
import {Redirect} from 'react-router-dom';
import {Button} from 'antd';
import Login from './login';
import About from './about';

const Welcome: React.FC = () => {
    const redir = (page: string) => {
        console.log('Page:', page);
        if (page === 'login'){
          // <Redirect to="/login"/>    
        } else if(page === 'about') {
            //<Redirect to="/about"/>
        } 
    }

    return (
         <div style={{textAlign: 'center'}}>
            <div style={{display:'inline-block'}}>

                    <Button
                        key="login"
                        type="primary"
                        onClick={() => redir('login')}
                    >Login Page</Button>

                    <Button
                        key="about"
                        type="primary"
                        style={{marginTop:'20px'}}
                        onClick={() => redir('about')}
                    >Navigation Bar</Button>

            </div>
        </div>
    );
}

export default Welcome;

about.tsx关于.tsx

import React from "react";

const About: React.FC = () => {
  return <div>About Page</div>;
};

export default About;

login登录

import React from 'react';

const Login: React.FC = () => {
  return (
    <div>Login Page</div>
  );
}

export default Login;

Based on routing configurations in App.tsx pages loading fine (localhost:3000/about or localhost:3000/login and localhost:3000/)基于 App.tsx 页面中的路由配置加载正常(localhost:3000/about 或 localhost:3000/login 和 localhost:3000/)

But the problem occurs when I tries to redirect to a page when clicking a button(In my real scenario, I have to navigate to welcome page once login is successful)..但是当我在单击按钮时尝试重定向到页面时会出现问题(在我的真实场景中,一旦登录成功,我必须导航到欢迎页面)。

Please clarify on following questions if possible since I am confused: 1. Routing works fine when landing in a page, how to manage it when I want to redirect to a page via code and url should reflect.如果可能,请澄清以下问题,因为我很困惑: 1. 登陆页面时路由工作正常,当我想通过代码重定向到页面时如何管理它,url 应该反映。 2. What is the best way to keep these kinda routing and will it support history when user clicks on back button of browser? 2. 保持这些路由的最佳方式是什么?当用户点击浏览器的后退按钮时,它是否支持历史记录?

Thank you.谢谢你。

You probably need history你可能需要历史

import { createHashHistory } from 'history'

export const history = createHashHistory({
    hashType: 'hashbang'
})

Later importing in inside file:稍后导入内部文件:

import { history } from '../helpers'

Put this line of code inside function responsible for authorization / authentication - after subbmiting - this line push user to the path is needed.将这行代码放在负责授权/身份验证的函数中 - 提交后 - 这行将用户推送到路径是需要的。

history.push('/login')

To navigate via a router on click, you should pass properties to component used in .要在单击时通过路由器导航,您应该将属性传递给 . To do that, replace const Welcome: React.FC = () => { with const Welcome: React.FC = (props) => {为此,请将 const Welcome: React.FC = () => { 替换为 const Welcome: React.FC = (props) => {

and in redir method instead而在 redir 方法中

 if (page === 'login'){
      // <Redirect to="/login"/>    
    } else if(page === 'about') {
        //<Redirect to="/about"/>
    } 

use

 if (page === 'login'){
    this.props.history.push("/login");
 } else if(page === 'about') {
    this.props.history.push("/about");
 } 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM