简体   繁体   English

单击 React 中的链接时页面重新加载

[英]Page reloads on click of a Link in React

I'm trying to show login and signup in just one page, that I can click on a login link that shows me the login form.我试图在一个页面中显示登录和注册,我可以单击显示登录表单的登录链接。

The problem is that when I click on the login link , <a href="" onClick={() => setlogin(true)}>login?</a> , the page gets loaded for 1s and then returns to sign up form.问题是当我点击登录链接时, <a href="" onClick={() => setlogin(true)}>login?</a> ,页面被加载 1s 然后返回注册形式。

Code:代码:

import React, { ReactElement, Fragment, useState } from "react";

function LoginSignUp() {
  const [login, setlogin] = useState(false);

  return (
    <Fragment>
      {login ? (
        <form className="form-signin">
          <input
            type="email"
            id="inputEmail"
            className="form-control"
            placeholder="Email address"
            required
          />
          <input
            type="password"
            id="inputPassword"
            className="form-control"
            placeholder="Password"
            required
          />
          <button type="submit">Sign in</button>
        </form>
      ) : (
        <form className="form-signup">
          <input
            type="text"
            name="username"
            className="form-control"
            placeholder="Username"
            required
          />
          <input
            type="email"
            name="email"
            className="form-control"
            placeholder="Email address"
            required
          />
          <input
            type="password"
            name="password"
            className="form-control"
            placeholder="Password"
            required
          />
          <input
            type="password"
            name="password2"
            className="form-control"
            placeholder="Confirm Password"
            required
          />
          <button type="submit">Sign Up</button>
        </form>
      )}
      <p id="logintxt">
        Do you already have an account? ,{" "}
        <a href="" onClick={() => setlogin(true)}>
          login?
        </a>
      </p>
    </Fragment>
  );
}

You can use preventDefault to stop the default behavior of hyperlink which refreshed your page and component's login state data became false again:您可以使用preventDefault来停止刷新页面和组件login state 数据再次变为false的超链接的默认行为:

Option 1:选项1:

function handleClick(e) {
  e.preventDefault()
  setlogin(true)
}

<a href="/" onClick={handleClick}>login?</a>

Option 2:选项 2:

Or, you can use # as hyperlink placeholder, it won't refresh the page but will just append a # in browser URL.或者,您可以使用#作为超链接占位符,它不会刷新页面,而只会在浏览器 URL 中使用 append 一个# But it may give you eslint warning that anchor is invalid :但它可能会给你eslint 警告锚点无效

<a href="#" onClick={handleClick}>login?</a>

Option 3:选项 3:

Or, you can use a span or button :或者,您可以使用spanbutton

<span onClick={handleClick}>login?</span>

// OR

<button type="button" onClick={handleClick} class="btn btn-link">login?</button>

... and, make it look like a link using your or bootstrap CSS. ...并且,使用您的或引导CSS 使其看起来像一个链接。

Also, don't leave it blank href="" , it will take you to the current URL location but will still refresh your page.此外,不要将其留空href="" ,它将带您到当前的 URL 位置,但仍会刷新您的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM