React 공홈 읽기 - Reference [useContext]

null·2024년 1월 9일
0

react 공홈 읽기.

목록 보기
1/3

https://react.dev/reference/react/createContext

useContext returns the context value for the calling component.

If there is no such provider, then the returned value will be the defaultValue you have passed to createContext for that context.
--> defaultValue를 설정 안 할수도 있나? 그렇다면 무엇을 반환하게 되는거지?

useContext() call in a component is not affected by providers returned from the same component. The corresponding <Context.Provider> needs to be above the component doing the useContext() call.

의견

  • 가장 가까운 Context.Provider 에 전달된 value에 따라 결정된다.
  • Provider는 항상 useContext의 상위에 있어야 한다.
  • 정해진 value가 없으면 스킵해도 되는거 아닌가? 했는데 value={undefined}로 value 명시가 필요하군요.
  • Context value 변경시 리렌더가 자주 일어나다 보니 이때는 useMemo, useCallback 을 적절히 사용해서 최적화 하자.
    가장 가까운 SomeContext.Provider에서 정의한 값을 리턴하고, 프로바이더가 없으면 defaultValue를 리턴한다.
  • Object.is() 로 비교했을 때 value가 변경된거면, 리액트는 모든 자식을 리렌더 시킨다.
  • symlinks가 뭐지??? --> 바로가기 같은 것.
  • memo를 사용한 경우에도 context의 새로운값(최신 값)을 자식에 전달한다. = memo로 렌더링을 건너뛰어도 새로운 값을 받는 것을 막을 수 없다. = 새로운 값은 받지만 리렌더는 안 한다는 것 ! !
profile
개발이 싫어.

0개의 댓글