티스토리 뷰
Web/nextjs
[reactjs/nextjs] cannot update a component (`MyApp`) while rendering a different component (`AuthticatedComponent`).
HAN_PY 2022. 10. 6. 22:16반응형
annot update a component (`MyApp`) while rendering a different component (`AuthticatedComponent`).
에러(경고) 발생

문제 원인
위와 같은 에러(경고)가 발생했다. 이는 useState를 변경하는 부분이 useEffect나 이벤트리스터에 의한 변화가 아닌 로직내부에 들어있어서 나는 에러이다. 즉, react는 props나 상태 변화에 따라 여러번 re-render를 한다. 그리고 개발단계에서는 re-render를 통해서 변화를 확인하기도 한다.
해결책
간단히 useEffect 내부에 넣어주면된다. 직관적으로 쉽게 예를 들겠다.
const Test = (prop) => {
const [data, setData] = useState('')
if (prop.flag){
setData('변경변경')
}
return (
<>
...
</>
)
}
export default Test;
위의 코드의 문제는 렌더가 되는 도중에 setData에 의해 데이터 변경이 일어난다는 것이다. 이를 방지하기 위해 useEffect() 내부에 넣어주어야 한다. 애래와 같이 적으면 해결된다.
const Test = (prop) => {
const [data, setData] = useState('')
useEffect(()=> {
if (prop.flag){
setData('변경변경')
}
}, [prop.flag])
return (
<>
...
</>
)
}
export default Test;
혹시나 위의 로직이 어렵다면, 아래의 내용들을 참고하자.
반응형
'Web > nextjs' 카테고리의 다른 글
[Nextjs] 전역 스타일 적용하기(global css) (0) | 2023.09.16 |
---|---|
[nextjs] next.config.js 내부의 Rewrite 적용 (0) | 2022.11.27 |
[nextjs] mongodb 연결 (0) | 2022.09.30 |
[nextjs] console.log가 두 번 찍히는 이유 (0) | 2022.09.24 |
[nextjs] next.config.js 내부의 swcMinifty 설정 (1) | 2022.09.21 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- UserCreationForm
- 자연어처리
- useState
- useHistory 안됨
- error:0308010C:digital envelope routines::unsupported
- Python
- next.config.js
- BFS
- nodejs
- Queue
- login
- react
- NextJS
- Express
- logout
- 자료구조
- vuejs
- mongoDB
- typescript
- react autoFocus
- nextjs autoFocus
- Deque
- 클라우데라
- read_csv
- TensorFlow
- DFS
- JavaScript
- Vue
- django
- pandas
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함