Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- CSS
- 이벤트 루프
- 코드스테이츠 메인프로젝트
- 재귀함수
- 유데미
- 백준 nodeJS
- sort
- 프론트엔드
- 자바스크립트
- Node js
- 백준
- kakao map api
- primitive type
- 코드스테이츠
- react js
- 자료구조
- next/Image
- OSI 7계층
- input class
- 코딩테스트
- 페이지네이션
- 배열
- 정규표현식
- Native select
- MUI
- JavaScript Deep Dive
- 알고리즘
- javascript
- nodejs
- 버블정렬
Archives
- Today
- Total
신입 개발자에서 시니어 개발자가 되기까지
Warning : A component is changing an uncontrolled input to be controlled(React JS) 본문
error & solution
Warning : A component is changing an uncontrolled input to be controlled(React JS)
Jin.K 2022. 10. 4. 19:50문제 코드
export default function App() {
const [username, setUsername] = useState("");
return (
<div className="App">
<div>{username}</div>
<input
type="text"
value={username}
onChange={(event) => setUsername(event.target.value)}
placeholder="여기는 인풋입니다."
className="tweetForm__input--username"
></input>)
input에서 value로 username을 참조했더니 컴포넌트가 uncontrolled input을 controlled로 바꾸고 있다는 경고가 뜬다. error가 아니라서 작동은 하지만 궁금해서 찾아봤다.
A component is changing an uncontrolled input of type text to be controlled error in ReactJS
Warning: A component is changing an uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a contro...
stackoverflow.com
결론 : value가 username을 참조하고 있는데 초기값이 undefined이므로 input field는 uncontrolled상태가 된다.(uncontrolled와 controlled가 무얼 의미하는지는 모르겠다) 그리고 값을 입력하면 controlled component로 전환되는데 이 때 warning이 발생하는 것이다. 해결책은 username값이 없을 때 따로 처리를 해주면 됨. value={username || ""} 이렇게.
'error & solution' 카테고리의 다른 글
git pull warning (you have divergent branches ...) (2) | 2023.04.30 |
---|---|
Cannot access before initialization 오류(feat. TDZ) (0) | 2022.10.27 |
fatal: not a git repository (or any of the parent directories) 오류 (0) | 2022.08.29 |