javascript - How to change login button to logout button in React - Stack Overflow

Hi I'm building a single page app using react.js for the first time, I'm struggling really ba

Hi I'm building a single page app using react.js for the first time, I'm struggling really badly with syntax here. I simply want to change the login button to a logout button when the user has signed in. Where am I going wrong?

render() {
  const isLoggedIn = this.state.isLoggedIn;

  return (
    if (isLoggedIn) {
      <div className="authentication">
        <button>Logout</button>
      </div>
    } else {
      <a href='http://localhost:8888'>
        <button>Login</button>
      </a>
    }
  );
}

Hi I'm building a single page app using react.js for the first time, I'm struggling really badly with syntax here. I simply want to change the login button to a logout button when the user has signed in. Where am I going wrong?

render() {
  const isLoggedIn = this.state.isLoggedIn;

  return (
    if (isLoggedIn) {
      <div className="authentication">
        <button>Logout</button>
      </div>
    } else {
      <a href='http://localhost:8888'>
        <button>Login</button>
      </a>
    }
  );
}
Share Improve this question edited Mar 7, 2020 at 9:28 Soroush Chehresa 5,6981 gold badge16 silver badges30 bronze badges asked Mar 7, 2020 at 8:50 TheMayerofTheMayerof 1933 gold badges4 silver badges17 bronze badges 1
  • 1 Have you posted your correct html syntax too? – Anurag Srivastava Commented Mar 7, 2020 at 8:53
Add a ment  | 

3 Answers 3

Reset to default 1

You should use the if statement before using the return:

render() {
  const { isLoggedIn } = this.state;

  if (isLoggedIn) {
    return (
      <div className="authentication">
        <button>Logout</button>
      </div>
    );
  }

  return (
    <a href='http://localhost:8888'>
      <button>Login</button>
    </a>
  );
}

The better way is using the ternary operator in the return block:

render() {
  const { isLoggedIn } = this.state;

  return (
    <div>
      {
        isLoggedIn 
          ? (
              <div className="authentication">
                <button>Logout</button>
              </div>
            )
          : ( 
              <a href='http://localhost:8888'>
                <button>Login</button>
              </a>
            )
      }
    </div>
  );
}

And the best way can be:

handleAuth = () => {
  const { isLoggedIn } = this.state;

  if(isLoggedIn) {
    // Do logout
  } else {
    // Do login
  }
}

render() {
  const { isLoggedIn } = this.state;

  return (
    <button onClick={handleAuth}>
      {isLoggedIn ? 'Logout' : 'Login'}
    </button>
  );
}

if is a statement and you can use statement after return you should use ternary operator.

Also you nesting button inside a. You should use href directly on button

render() {
     const isLoggedIn = this.state.isLoggedIn;

    return (
      isLoggedIn ? 
      <div className="authentication">
        <button >Logout </button>
      </div>
      : 
      <button href='http://localhost:8888'>Login </button>
     )
}

You can create some function ponents and use it like:

function LogoutBtn(props) {
  return <div className="authentication"><button >Logout </button></div>;
}

function LoginBtn(props) {
  return <a href='http://localhost:8888'><button>Login </button></a>;
}

function AuthBtn(props) {
  const isLoggedIn = props.isLoggedIn;
  if (isLoggedIn) {
    return <LogoutBtn />;
  }
  return <LoginBtn />;
}

and then inside render method use it like:

render() {
    const {isLoggedIn} = this.state;
    return <AuthBtn isLoggedIn={isLoggedIn} />;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745464385a4628852.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信