react-tutorial icon indicating copy to clipboard operation
react-tutorial copied to clipboard

4. TypeScript 와 Context API 활용하기 · GitBook

Open utterances-bot opened this issue 4 years ago • 2 comments

4. TypeScript 와 Context API 활용하기 · GitBook

https://react.vlpt.us/using-typescript/04-ts-context.html

utterances-bot avatar Nov 27 '20 12:11 utterances-bot

SampleStateContext를 정의할 때, createContext<State>({count: 0, ...}) 식으로 기본값을 지정 해 주고, useSampleState(): State {...} 으로 return type을 정의 해 주면, null check를 하지 않아도 됩니다.

마찬가지로 SampleDispatchContext를 정의할 때, createContext<SampleDispatch>(() => null)로 해 주고 useSampleDispatch(): SampleDispatch {...}으로 return type을 정의 해 주면, null check가 필요하지 않게 됩니다.

jung-hunsoo avatar Nov 27 '20 12:11 jung-hunsoo

꼭 null check가 필요하진 않겠지만 Provider의 영향 범위를 벗어난 곳에서 context를 소비하는 커스텀 훅을 잘못사용하게 될 경우를 인지시켜주는 측면에서 null 체크를 통한 예외 발생도 유효할 것 같아요~ (개인적인 의견입니다~)

Yuddomack avatar Mar 13 '22 10:03 Yuddomack