Skip to content
UseRef VS학습 기록

Sign up and login: useRef vs useState, which is more efficient?

1 min readLearningMoved from my Tistory blogRead the Korean original

Today I built the form data that login and sign-up send to the backend, and tested that the values show up correctly when the login or sign-up button is pressed.

tsx
export default function Login({show, setShowLoginModal}: PaymentModalProps) {
  const [joinForm, setJoinForm] = useState(false)
  const [joinWelcomeText, setJoinWelcomeText] = useState('Compass에 오신 것을 환영합니다.')

  const loginEmailRef = useRef<HTMLInputElement>(null)
  const loginPasswordRef = useRef<HTMLInputElement>(null)

  const joinWelcomeTextRef = useRef<HTMLDivElement>(null)
  const joinEmailRef = useRef<HTMLInputElement>(null)
  const joinPasswordRef = useRef<HTMLInputElement>(null)
  const joinPassword2Ref = useRef<HTMLInputElement>(null)
  const joinNickNameRef = useRef<HTMLInputElement>(null)

  const handleLogin = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault()
    console.log('이메일 : ', loginEmailRef.current?.value)
    console.log('비밀번호 : ', loginPasswordRef.current?.value)
    setShowLoginModal(false)
  }

  const handleJoin = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault()
    if (joinPasswordRef.current?.value !== joinPassword2Ref.current?.value) {
      if (joinWelcomeTextRef.current) {
        joinWelcomeTextRef.current.style.color = 'red'
      }

      setJoinWelcomeText('비밀번호가 일치하지 않습니다.')
      return
    }

    console.log('이메일 : ', joinEmailRef.current?.value)
    console.log('비밀번호 : ', joinPasswordRef.current?.value)
    console.log('닉네임 : ', joinNickNameRef.current?.value)
    setJoinForm(false)
    setShowLoginModal(false)
  }

  return (
    <ModalBackdrop show={show}>
      <ModalContent>
        {joinForm ? (
          <JoinForm onSubmit={e => handleJoin(e)}>
            <ModalCloseTitleBox>
              <CloseIcon
                onClick={() => {
                  setShowLoginModal(false), setJoinForm(false)
                }}
              />
              <ModalTitle>회원가입</ModalTitle>
            </ModalCloseTitleBox>
            <Line />
            <WelcomeText ref={joinWelcomeTextRef}>{joinWelcomeText}</WelcomeText>
            <InputGroupJoin>
              <Input
                type='email'
                pattern='[a-zA-Z0-9]+[@][a-zA-Z0-9]+[.]+[a-zA-Z]+[.]*[a-zA-Z]*'
                placeholder='이메일'
                required
                ref={joinEmailRef}
              />
              <Input type='password' placeholder='비밀 번호' required ref={joinPasswordRef} />
              <Input type='password' placeholder='비밀 번호 확인' required ref={joinPassword2Ref} />
              <Input type='text' placeholder='닉네임' required ref={joinNickNameRef} />
            </InputGroupJoin>
            <LoginButton type='submit'>회원가입</LoginButton>
          </JoinForm>
        ) : (
          <LoginForm onSubmit={e => handleLogin(e)}>
            <ModalCloseTitleBox>
              <CloseIcon
                onClick={() => {
                  setShowLoginModal(false), setJoinForm(false)
                }}
              />
              <ModalTitle>로그인</ModalTitle>
            </ModalCloseTitleBox>
            <Line />
            <WelcomeText>Compass에 오신 것을 환영합니다.</WelcomeText>
            <InputGroup>
              <Input
                type='email'
                pattern='[a-zA-Z0-9]+[@][a-zA-Z0-9]+[.]+[a-zA-Z]+[.]*[a-zA-Z]*'
                placeholder='이메일'
                required
                ref={loginEmailRef}
              />
              <Input type='password' placeholder='비밀 번호' required ref={loginPasswordRef} />
            </InputGroup>
            <JoinFindPassBox>
              <button onClick={() => setJoinForm(true)}>회원가입</button>
              <TbSlash />
              <button>비밀번호 찾기</button>
            </JoinFindPassBox>
            <LoginButton type='submit'>로그인</LoginButton>
            <Hrspan>또는</Hrspan>
            <KaKaoLoginButton>
              <ImBubble />
              카카오 로그인
            </KaKaoLoginButton>
          </LoginForm>
        )}
      </ModalContent>
    </ModalBackdrop>
  )
}

First, I used useRef instead of useState in the code above because state can cause needless re-renders. useRef doesn't trigger a render when its value changes, so I thought it would be much more efficient.

Login
Figure 1. Login
Sign up
Figure 2. Sign up
  • #useRef
  • #useState
  • #login
  • #sign up

More in Learning