인프런 커뮤니티 질문&답변

uoon lee님의 프로필 이미지

작성한 질문수

풀스택 리액트 토이프로젝트 - REST, GraphQL (for FE개발자)

ReactQuery 환경세팅

query client를 clientRef.current에 저장하는 이유가 무엇인가요~?

해결된 질문

21.12.03 10:28 작성

·

293

0

new QueryClient()

위 객체를 useState의 setState()로 state에 저장할 수도 있는데 useRef를 써서 ref.current에 저장하는 이유가 따로 있는지 궁금합니다! useRef는 html tag의 reference를 참조할 때 쓰는 걸로 알고 있어서요 :)

답변 1

1

정재남님의 프로필 이미지
정재남
지식공유자

2021. 12. 03. 11:06

useRef는 html tag의 reference를 참조할 때에만 쓰는 것이 아닙니다.

current에 언제나 최신정보가 반영되길 기대하면서,

동시에 current의 값이 변경되더라도 재랜더링을 일으키지 않길 원하는 모든 경우에

어떤 데이터든 넣을 수 있습니다.

 

https://reactjs.org/docs/hooks-reference.html#useref

Keep in mind that useRef doesn’t notify you when its content changes. Mutating the .current property doesn’t cause a re-render. If you want to run some code when React attaches or detaches a ref to a DOM node, you may want to use a callback ref instead.

 

uoon lee님의 프로필 이미지
uoon lee
질문자

2021. 12. 03. 11:49

그렇군요! 공식문서를 꼼꼼히 읽어봐야겠네요. 답변해주셔서 감사합니다 :D

uoon lee님의 프로필 이미지

작성한 질문수

질문하기