리액트 렌더링 에러

nevermind·2023년 1월 13일
0

에러

목록 보기
1/1


Warning: Cannot update a component (ThemeProvider) while rendering a different component (~).

contextAPI로 컴포넌트를 렌더링하는동안 다른 컴포넌트를 업데이트할 수 없다는 것이었다.

That you shouldn’t setState during render.
I ran into this recently and the fix was wrapping setState handler call sites in useEffect

렌더링 중에 setState를 사용하는 대신 useEffect안에서 상태가 변했을 때를 감지하여 setState를 실행해주니 문제가 해결되었다.

왜 useEffect를 사용해야할까?

공식문서
What does useEffect do? By using this Hook, you tell React that your component needs to do something after render. React will remember the function you passed (we’ll refer to it as our “effect”), and call it later after performing the DOM updates. In this effect, we set the document title, but we could also perform data fetching or call some other imperative API.

render 이후의 작업을 수행하기 위해서는 useEffect가 필요하다


출처: https://github.com/facebook/react/issues/18178

profile
winwin

0개의 댓글