반응형
judahhh
좋게 생각하면 좋게 흘러간다!
judahhh
전체 방문자
오늘
어제
  • 분류 전체보기 (90)
    • git (2)
    • Algorithm (39)
    • Computer Science (8)
    • TIL (8)
    • FrontEnd (3)
      • React (8)
      • Typescript (0)
      • Javascript (13)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • branch 생성
  • 주니어 개발자
  • 자바스크립트 배열 함수
  • branch 삭제
  • 자바스크립트
  • 티스토리챌린지
  • git branch 명령어
  • 오블완
  • branch 이동
  • git branch
  • 프론트엔드 개발자
  • 이직
  • 자바스크립트 배열 메소드
  • branch 조회

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
judahhh

좋게 생각하면 좋게 흘러간다!

[React] button onClick 시 onSubmit 이 실행되는 문제 해결
FrontEnd/React

[React] button onClick 시 onSubmit 이 실행되는 문제 해결

2023. 5. 11. 18:13
728x90

 


 

sample image

 

다음과 같이 회원가입 기능을 개발하던 중 인증코드와 인증코드 확인을 누르면 이 form 을 감싸고 있는 곳에 선언해둔 onSubmit 이 실행되어 비밀번호를 입력하지 않았다고 뜨는 문제가 발생하였다.

 

인증코드 전송버튼과 인증코드 확인 버튼은 onClick을 통해 이벤트를 발생시키도록 하였고 폼 전체의 전송은 onSubmit을 통해 이벤트가 발생한다. 

 

button 에 onClick 발생 시 onSubmit이 발생하는 이유를 Stack overflow 를 통해 찾아봤더니

 onsubmit의 제일 밑에 기본이 되는 함수가 onClick 이어서 라고 한다.

 

 

 

이를 해결하기위해 <button></button>으로 만든 버튼을 <input type="button"/> 으로 변경해주면 된다고 한다.

나는 button을 styled-components를 통해 만들었기 때문에 이부분을 input으로 바꿔주어야 했다.

 

 

 

 

 

수정 전 코드

const StyleEmailConfirmBtn = styled.button`  //이 부분 수정
  width: 100px;
  height: 40px;
  border: solid 1px #ebd500;
  font-family: "Noto Sans KR", sans-serif;
  background-color: white;
  color: #ebd500;
  border-radius: 10px;
  margin-bottom: 10px;
  :hover {
    cursor: pointer;
  }
`;

 

 

수정 후 코드

const StyleEmailConfirmBtn = styled.input.attrs({ type: "button" })` //이 부분 수정
  width: 100px;
  height: 40px;
  border: solid 1px #ebd500;
  font-family: "Noto Sans KR", sans-serif;
  background-color: white;
  color: #ebd500;
  border-radius: 10px;
  margin-bottom: 10px;
  :hover {
    cursor: pointer;
  }
`;

 

 

 

수정 후에는 버튼을 onClick 해도 폼 전체가 onSubmit 되는 문제가 해결되었다!!!

button을 선언하는 방법이 두가지인데 어떻게 선언하느냐에 따라 다른 결과를 낼 수 있다는 점을 다시 한번 배우게 되었다 😎

 

 

 

 

 

 

참고자료

https://stackoverflow.com/questions/56378356/how-do-i-convert-css-to-styled-components-with-inputtype-submit-attribute

 

How do I convert CSS to Styled-Components with input[type="submit"] attribute?

How do I convert standard CSS with special attributes like "type" and "active", to Styled-Components consts? .box { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%...

stackoverflow.com

 

 


 

728x90
    'FrontEnd/React' 카테고리의 다른 글
    • 프로그래머스 lv1. 푸드 파이트 대회 풀이
    • [React] 컴포넌트 리렌더링(Re-Rendering) 조건
    • [React] 환경변수가 undefined일 때 (process.env undefined)
    • [React] 오늘의 에러 - Too many re-renders. React limits the number of renders to prevent an infinite loop.
    judahhh
    judahhh
    프론트엔드 개발자의 이모저모

    티스토리툴바