-
[TS] 엔터쳐서 실행하기 유틸함수 만들기Java Script & Type Script/Favorites 2023. 7. 4. 09:35728x90
e.key 가 "Enter" 일 때 실행 할 함수를 넣을 수 있다.
const enterPressHandler = (e: any) => { if (e.key === "Enter") { searchHandler(); // 이게 실행할 함수 } }; ... <StInputBox.SearchInput placeholder="검색 키워드를 입력해주세요." onChange={(e) => setSearchKey(e.target.value)} value={searchKey} onKeyDown={enterPressHandler} />
+ 23/12/24 추가
export const enterToProcess=( e : any, func: ()=>void )=>{ if (e.key === "Enter") { func(); } };
내 생각엔, 이렇게 util 함수로 빼놓고 파일 전체에서 불러서 써도 될 것 같다.
다져다 쓸 땐 이런식으로!!
<button onKeyDown={(e)=> enterToProcess( e, loginHandler )}>로그인<button>
728x90'Java Script & Type Script > Favorites' 카테고리의 다른 글
[React] 리액트 스토리북 도입하기 (Feat. Atomic pattern) (1) 2024.01.06 [REACT] 데시벨측정 자동녹음종료기능 custom hook (0) 2023.06.25 [React] 네이버 자동로그인 따라하기 (0) 2023.06.19 [Library] React-slick, Apex-charts 라이브러리 이용법 -리액트로 캐러셀 구현, 도넛그래프, 반원그래프, 오각형그래프, 막대그래프 그리기 (0) 2023.06.15 [React] 리팩토링 - 컴포넌트 역할 분리 (0) 2023.04.20