React Router Link不刷新页面

问题描述 投票:0回答:1

[早上好,我有一个带有导航栏的标头组件,并且其中有一些来自React Router的链接,该标头在我的应用程序组件中被调用,并且这些链接具有一些与身份验证有关的显示条件。问题是,当我单击标题上的任何链接时,页面会更改,但标题本身保持不变,例如,登录名,单击登录时的注销应该隐藏,并且显示注销组件,但它不起作用。附:当我改变一切时,一切正常

function TodoApp() {
//   let isUserLoggedIn = AuthenticationService.isUserLoggedIn();
//   console.log(isUserLoggedIn);
  //   render() {
  return (
    <div className="TodoApp">
     { <Router>
        <div>
          <HeaderComponent isUserLoggedIn={AuthenticationService.isUserLoggedIn} />
          <Switch>
            <Route path="/" exact component={withRouter(LoginComponent)} />
            <Route path="/login" component={withRouter(LoginComponent)} />
            <AuthenticatedRoute
              path="/welcome/:name"
              component={withRouter(WelcomeComponent)}
            />
            <AuthenticatedRoute
              path="/todos"
              component={withRouter(ListTodosComponent)}
            />
            <AuthenticatedRoute
              path="/logout"
              component={withRouter(LogoutComponent)}
            />
            <Route component={withRouter(ErrorComponent)} />
          </Switch>
          <FooterComponent />
        </div>
     </Router>
      {/* <LoginComponent /> */}
    </div>
  );
  }
}

class HeaderComponent extends Component {
  // constructor(props) {
  //   super(props);
  //   this.state = {
  //     isUserLoggedIn: AuthenticationSevice.isUserLoggedIn(),
  //   };
  //   //  this.handleState = this.handleState.bind(this);
  // }

  componentDidMount() {
    console.log("Header charged");
  }

  //   handleState = () => {
  //     this.setState(() => {
  //         return { isUserLoggedIn: AuthenticationSevice.isUserLoggedIn() };
  //       });
  //       console.log(this.state.isUserLoggedIn);
  //   }

  render() {
    // const isUserLoggedIn = AuthenticationSevice.isUserLoggedIn();
    return (
      <header>
        <nav className="navbar navbar-expand-md navbar-dark bg-dark">
          <div>
            <a className="navbar-brand" href="https://github.com/sbouhaddi">
              Sbouhaddi
            </a>
          </div>
          <ul className="navbar-nav">
            {this.props.isUserLoggedIn && (
              <li>
                <Link className="nav-link" to="/welcome/Sbouhaddi">
                  Home
                </Link>
              </li>
            )}
            {this.props.isUserLoggedIn && (
              <li>
                <Link className="nav-link" to="/todos">
                  Todos
                </Link>
              </li>
            )}
          </ul>
          <ul className="navbar-nav navbar-collapse justify-content-end">
            {!this.props.isUserLoggedIn && (
              <li>
                <Link className="nav-link" to="/login">
                  Login
                </Link>
              </li>
            )}
            {this.props.isUserLoggedIn && (
              <li>
                <Link
                  className="nav-link"
                  onClick={AuthenticationSevice.logout}
                  to="/logout"
                >
                  Logout
                </Link>
              </li>
            )}
          </ul>
        </nav>
      </header>
    );
  }
}
reactjs hyperlink routing routes react-router
1个回答
0
投票
未出现,您调用您的AuthenticationService登录函数将值传递给isUserLoggedIn属性。
© www.soinside.com 2019 - 2024. All rights reserved.