单击导航链接后,是否重定向到以前的状态?

时间:2019-02-25 02:50:17

标签: reactjs state react-props

因此,我有一个导航栏,一旦用户单击“注销”,它就会呈现非登录用户看到的导航栏。到目前为止,我对此感到失望,但是我的问题是,当我单击网页上的其他单击之一(例如“关于”)时,导航栏将恢复为“注销”状态。我该如何解决这个问题?我认为这是因为在我的App.js源代码中,userId: 123(测试目的)是经过硬编码的,并且始终运行此代码。

CustomNavbar.js

import React, { Component } from 'react'
import { Navbar, Nav, NavItem } from 'react-bootstrap';

class CustomNavbar extends Component {

    render() {
        if (this.props.userID === null) {
            return (
                <div>
                    <Navbar collapseOnSelect expand="md" bg="dark" variant="dark">
                        <Navbar.Brand href="/">
                            Ndnu's Notes
                        </Navbar.Brand>
                        <Navbar.Toggle aria-controls="responsive-navbar-nav" />
                        <Navbar.Collapse id="responsive-navbar-nav">
                        </Navbar.Collapse>
                    </Navbar>
                    <Navbar className="footer text-center" fixed="bottom" expand="lg" bg="light">
                        <Nav.Link href="/contactus">Contact Us</Nav.Link>
                        <Nav.Link href="/privacyterms">Privacy & Terms</Nav.Link>
                        <Nav.Link href="/about">About</Nav.Link>
                    </Navbar>
                </div>
            )
        } else {    
            return (
                <div>
                    <Navbar collapseOnSelect expand="md" bg="dark" variant="dark">
                        <Navbar.Brand href="/">
                        Ndnu's Notes
                    </Navbar.Brand>
                    <Navbar.Toggle aria-controls="responsive-navbar-nav" />
                        <Navbar.Collapse id="responsive-navbar-nav">
                            <Nav className="mr-auto">
                            </Nav>
                            <Nav>
                                <Nav.Link eventKey={2} href="/myAccount"> <i class="fas fa-user"></i> My Account</Nav.Link>
                                <Nav.Link eventKey={1} href="/" onClick={this.props.logUserOut}> <i class="fas fa-sign-out-alt"></i> Log Out</Nav.Link>
                            </Nav>
                        </Navbar.Collapse>
                    </Navbar>

                    {/* Footer */}
                    <Navbar fixed="bottom" bg="light">
                        <Nav.Link href="/contactus">Contact Us</Nav.Link>
                        <Nav.Link href="/privacyterms">Privacy & Terms</Nav.Link>
                        <Nav.Link href="/about">About</Nav.Link>
                    </Navbar>
                </div>
            )
        }
    }
}
export default CustomNavbar

App.js

import React from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import LoginAndRegister from './LoginAndRegister'
import ContactUs from './Components/ContactUs.js';
import PrivacyTerms from './Components/PrivacyTerms.js';
import CustomNavbar from './Components/CustomNavbar.js';
import About from './Components/About.js';
import Table from './Components/Table.js';

class App extends React.Component {

constructor() {
    super();
    this.state = {
        userID: 123
    }
}
logUserOut = (e) => {
    e.preventDefault()
    this.setState({userID: null});
}

render() {
    return (
        <Router>
            <div>
                <CustomNavbar userID={this.state.userID} logUserOut={this.logUserOut} />
                <Route exact strict path="/contactus" component={ContactUs} />
                <Route exact strict path="/about" component={About} />
                <Route exact strict path="/privacyterms" component={PrivacyTerms} />
                  {!this.state.userID && <LoginAndRegister userID={this.state.userID}/>}
                <Table />
            </div>
        </Router>
    )
}
}
    export default App;

1 个答案:

答案 0 :(得分:1)

最好将用户登录状态保持在本地存储状态,而不是签入状态。并检查用户是否已登录以呈现所需的输出。

您将必须将用户信息保留在某个中央位置,以便可以在某些嵌套组件上轻松访问它,而不是像传递道具一样传递状态。

其他选项可以是上下文api,用于访问用户登录状态。

在页面上重新加载后似乎没有state.userID值。在您的代码中,您要在构造函数中设置userID,这将被称为组件生命周期的安装阶段的老化。

如有任何查询,请向我发送消息:)