일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 로컬
- 2번실행
- Git
- nextjs
- onkeypress
- typescript
- useEffect
- event type
- 원격
- 알파테스트
- reactStrictMode
- ts2322
- undefine
- delete branch
- branch
- OBT
- changeEvent
- react에러
- StrictMode
- onchange
- console.log
- useRef에러
- FGT
- KeyboardEvent
- 게임용어
- useref
- MutableRefObject
- reactjs
- 깃 브랜치 삭제
- react
- Today
- Total
목록React (3)
min.js

react useEffect를 사용하는데 로그가 이상하게 두번씩 찍힌다.🤔 🤔 dependency array에 빈 배열만 넣어주게 되면 컴포넌트가 마운트 되었을 때만 실행된다. (마운트 되었다는 의미는, DOM 객체가 생성되고 브라우저에 해당 컴포넌트가 나타나는 것) 그 이후에 리렌더링 되어도 해당 로그는 찍히지 않는다. useEffect(() => { console.log('useEffect'); }, []); 처음 페이지 로딩 시, useEffect가 두번씩 찍혀서 찾아보니 next.config.js에서 reactStrictMode: false로 추가 또는 변경하니 이제 한 번만 찍히게 된다! const nextConfig = { reactStrictMode: false, }; 참고
react useRef 사용하면서 자주 겪는 에러 TS2322 MutableRefObject 해결방법 useRef를 사용하고 나니 아래와 같은 에러와 함께 빨간줄이 그어졌다.. Type 'MutableRefObject' is not assignable to type 'LegacyRef | undefined'. Type 'MutableRefObject' is not assignable to type 'RefObject'. Types of property 'current' are incompatible. Type 'undefined' is not assignable to type 'HTMLDivElement | null'.ts(2322) index.d.ts(125, 9): The expected type c..

버튼을 클릭하면 메시지 전송이 되지만, 키보드의 엔터키를 눌렀을 때도 전송이 되게 하기 위해서는, input의 onKeyPress 속성 사용! 자세한 건 아래 코드에서 확인:) import React, { useRef, useState, ChangeEvent, KeyboardEvent } from 'react'; function InputBox() { const [text, setText] = useState(''); const inputRef = useRef(null); const onChange = (e: ChangeEvent) => { setText(e.target.value); }; const handleMessage = () => { if (text === '') { return alert('전..