如何在反应中更新导航栏

Sun*_* An 5 reactjs react-router

我正在开发反应项目,我的导航栏有一个登录按钮

在 Navigation.js 中,我检查用户是否登录

const authenticated = user != null;
Run Code Online (Sandbox Code Playgroud)

并决定显示登录或注销

{authenticated ? (
        <LogoutButton logout={logout} />
      ) : (
          <Link to="/login">
            <button>Login</button>
          </Link>

        )}
Run Code Online (Sandbox Code Playgroud)

但问题是登录后,导航栏没有更新为注销。我怎么解决这个问题?

Bab*_*obi 2

在您的导航类中

constructor(props) {
    super(props);
    this.state = {
        authenticated : null
    };
};

componentDidMount() {
    // Call this function here or anywhere like user login event
    this.setState({authenticated : user != null});
}
Run Code Online (Sandbox Code Playgroud)