簡體   English   中英

在注銷Reactjs上更新NavBar

[英]Updating NavBar on Logout Reactjs

我正在嘗試在注銷時更新NavBar,這是我編寫的代碼:

App.js:

class App extends Component {
  constructor(props){
    super(props);
    this.state = {};  
    this.handleLogout = this.handleLogout.bind(this); 
  }

  handleLogout(){
    sessionStorage.clear(); 
    this.setState({loggedIn: false});
  }

  componentWillMount(){
      if(sessionStorage.getItem('access_token') != null && sessionStorage.getItem('id_token') != null){
          this.setState({loggedIn: true}); 
      }
      else{
          this.setState({loggedIn: false}); 
      } 
  }
  render() {
    return (
        <BrowserRouter>
          <div>
            <title>Webshop</title> 
            <NavBar loggedIn={this.state.loggedIn} />
            <Switch>
                    {/*Routes need to be include in App.js otherwise root can't find the paths*/}
                    <Route exact path='/' component={Home}/>
                    <Route exact path='/categories' component={Categories}/>
                    <Route exact path='/login' component={Login}/>
                    <Route exact path='/register' component={Register}/>
                    {(this.state.loggedIn) ? 
                    <Route exact path='/logout' render={(props) => (<Logout logOutHandler={this.handleLogout} {...props}/>)} /> 
                    : null}
                    <Route render={function(){
                        return (<NotFound/>); 
                    }}/>
                </Switch>
              <Footer/>
          </div>  
        </BrowserRouter>
    );
  }
}

NavBar.js:

class NavBar extends Component {
    constructor(props) {
        super(props);
        this.loggedIn = this.props.loggedIn; 
        this.toggle = this.toggle.bind(this);
        this.state = {
          isOpen: false
        }; 
    }

    toggle() {
        this.setState({
          isOpen: !this.state.isOpen
        });
    }

    render(){
        return(
            <div>
                <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'/>
                <Navbar color="faded" light expand="md">
                    <NavLink className='navbar-brand' exact to='/'>
                        <img src={logo} alt='Brand' width='35px' height='35px'/>
                    </NavLink>
                    <NavbarToggler onClick={this.toggle} />
                    <Collapse isOpen={this.state.isOpen} navbar>
                        <Nav className="mr-auto" navbar>
                            <NavItem>
                                <NavLink className='nav-link' exact to='/categories'>
                                    Categories
                                </NavLink>
                            </NavItem>
                        </Nav>
                        <Nav className='mx-auto' navbar>
                            <Form inline>
                                <FormGroup>
                                    <Input size='sm' type="text" name="search" placeholder="Search" />
                                </FormGroup>
                                <Button size='sm'><i className='fa fa-search'></i></Button>
                            </Form>
                        </Nav>
                        <Nav className="ml-auto" navbar>

                            <NavItem>
                                <NavLink className='nav-link' exact to='/cart'>
                                    <i className='fa fa-shopping-cart'></i>
                                </NavLink>
                            </NavItem>

                            {(this.loggedIn) ?
                            <NavItem>
                                <NavLink className='nav-link' exact to='/profile'>
                                    <i className='fa fa-user'></i> 
                                </NavLink>
                            </NavItem>
                            : null }

                            {(this.loggedIn) ?

                            <NavItem>
                                <NavLink className='nav-link' exact to='/logout'>
                                    <i className='fa fa-sign-out'></i>
                                </NavLink>
                            </NavItem>
                            : 
                            <NavItem>
                                <NavLink className='nav-link' exact to='/login'>
                                    <i className='fa fa-sign-in'></i>
                                </NavLink>
                            </NavItem>
                            }
                        </Nav>
                    </Collapse>
                </Navbar>
            </div>
        );
    }
}

LogOut.js:

class Logout extends Component{
    constructor(props){
        super(props);

        this.handleLogout = this.handleLogout.bind(this); 

    }

    handleLogout(){
        sessionStorage.clear();
        this.props.logOutHandler(); 
        //window.location.replace('/login'); 
    }

    render(){
        return(
            <div>
                <Container className="content-container">
                    <Row className="justify-content-md-center">
                        <Col md={4}>
                            <Card>
                                <CardBody>
                                    <img className="logoutImage" src={logo} width='130' height='130px' alt='Logo'/>
                                    <hr></hr>
                                    <CardText>
                                        Clicking "Logout" will log you out from webshop [NAME]
                                    </CardText>
                                    <Link exact to='/'>
                                    <Button size='sm' className='float-left' color='default'>Cancel</Button>
                                    </Link>
                                    <Link excat to='/login'>
                                        <Button size='sm'className='float-right' color='secondary' onClick={this.handleLogout}>Logout</Button>
                                    </Link>
                                </CardBody>
                            </Card>
                        </Col>
                    </Row>
                </Container>

            </div>
        )
    }
}

我正在嘗試將狀態更新為LoginIn = false。 單擊Logout.js中的注銷按鈕。 這應該觸發方法handleLogout,該方法將觸發this.props.logOutHandler中的方法,該方法將觸發App.js內部的handleLogout,該方法應將狀態更新為loggingIn = false。 但是以某種方式,loginIn =始終為true,直到刷新頁面為止。

可能我可以使用window.location.replace('/ login')處理此錯誤,但是我想要一個無需刷新頁面而僅重新呈現NavBar組件的解決方案

在您的NavBar.js ,不需要在構造函數中執行this.loggedIn = this.props.loggedIn ,這可能會導致問題。

您可以直接在render訪問道具:

this.props.loggedIn ? {node} : null

代替:

this.loggedIn ? {node} : null

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM