I have worked on a sign in page and this is a page for controlling unauthorized access.
(我已经在登录页面上工作,这是一个用于控制未经授权的访问的页面。)
But, Page redirection is not working here, (但是,页面重定向在这里不起作用,)
Here is my code
import React from 'react';
import { Layout, Menu, Breadcrumb, Button } from 'antd';
import { BrowserRouter, Link, Redirect } from 'react-router-dom';
const { Header, Content, Footer } = Layout;
class MainPage extends React.Component {
constructor(props) {
super(props);
}
state= {
redirect: false,
}
render()
{
let users=localStorage.getItem('user');
function logOut() {
if (true) {
localStorage.clear();
this.props.history.push('/Signin');
}
}
function AuthPage () {
return(
<div>
{
(users) ? (<Layout>
<Content>
<div className="main-content">
<Link to="/Profile">Profile {users}</Link> |
<Link to="/Topics">Topics</Link> |
<Button onClick={logOut} type="link" >Logout</Button>
</div>
</Content>
</Layout>
) : (<div>Need to login</div>
)
}
</div>
);
}
return(AuthPage());
}
}
export default MainPage;
I also tried this:
(我也试过这个:)
This is Also Not working in redirection.
(这在重定向中也不起作用。)
import React from 'react'
import { Redirect } from 'react-router-dom'
const ProtectedComponent = () => {
if (authFails)
return <Redirect to='/Signin' />
}
return <div> My Protected Component </div>
}
What is the proper way of implementing Redirect in React.js.
(在React.js中实现重定向的正确方法是什么?)
ask by Dhamotharan translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…