The Basic Cheatsheet
์ ์ ์กฐ๊ฑด
์ ์ ์กฐ๊ฑด์ ๋ณด๋ฉด์ ์๊ฒ ๋ ์ฌ์ค ์ ๋ฆฌ
TypeScript์ ์ฅ์
TypeScript๋ฅผ ์ด๋ค๋ฉด JavaScript์์ unit test๋ฅผ ์ธ ํ์๊ฐ ์๋ค. TypeScript๊ฐ ์๋์ ์ผ๋ก ์ฒดํฌํด ์ค๋ค.
This is especially useful when youโre using a UI library and need to transform data. For example, if youโre using React, youโll need to transform data in state updates.
we can use mapped types like Readonly to convert one type to another type.
// Readonly<...> makes each property readonly
type Todo = Readonly<{
id: number
text: string
done: boolean
}>
// ์ <...>์ ์๋์ ๊ฐ๋ค.
type Todo = {
readonly id: number
readonly text: string
readonly done: boolean
}readonly๋ array์ push์๋ ๊ฐ์ด ์ฐ์ผ ์ ์๋ค.
Type assertions
์ถ์ฒ https://ts.chibicode.com/todo/
Function Components
Hooks
useState
default value์ธ null๊ณผ ํจ๊ป |๋ฅผ ์ฌ์ฉํ๋ union type์ ๋ง์ hook๋ค์ ์ฌ์ฉํ๋ค.
useRef
initial value๊ฐ ์๋ ref container ์์ฑํ ๋
ref1๋ readonly์ฉ react๊ฐ ๊ด๋ฆฌํ built-in ref attributes์ ์ ๋ฌํ๊ธฐ ์ํ ๊ฒ? (Response handles for your current value setting)
ref2๋
ref2.current๋ฅผ mutableํ๊ฒ ํด์ฃผ๊ณ ์ฌ์ฉ์๊ฐ ์ง์ ๊ด๋ฆฌํ๋instance variables๋ฅผ ์ํ ๊ฒ์ด๋ค.
๋ค? ๋ญ๋ผ๊ตฌ์ฌ???
useEffect
ํจ์ ๋๋ undefined ์ด์ธ์ ๋ค๋ฅธ ๊ฒ์ด return ๋์ง ์๋๋ก ์ฃผ์ํ๋ค.
setTimeout์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ number๊ฐ return๋๊ธฐ ๋๋ฌธ์ curly brace๋ก ๊ผญ ๊ฐ์ธ์.
useRef
useReducer
reducer action์ Discriminated Unions ์ฌ์ฉํ๋ค. return type์ ์ ์ํ๋ ๊ฒ์ ์ ๋ ์์ง๋ง์.
Discriminated Unions(์ ๋์จ ์๋ณ)์ singleton types, union types, type guards, ๋ฐ type aliases์ ๊ฒฐํฉํ์ฌ discriminated unions, tagged unions, ๋๋ algebraic(๋์์) data type์ผ๋ก ๋ถ๋ฆฌ๋ ๊ณ ๊ธ ํจํด์ ๋น๋ํ ์ ์๋ค.
๊ฒฐํฉํ interface๋ฅผ ์ ์ธํ๋ค.
๊ณตํต์ ์ผ๋ก kind๋ผ๋ ํ๋กํผํฐ๋ฅผ ๊ฐ์ง๊ณ ์๋๋ฐ ์ด๋ฅผ discriminant ํน์ tag๋ผ๊ณ ๋ถ๋ฅธ๋ค.
๊ฒฐํฉ์ฐ
discriminated union์ ์ฌ์ฉํ์!
useReducer์์๋ type์ด discriminant ํน์ tag๊ฐ ๋๋ ๊ฒ์ด๊ณ
์ด์ ์ ๋ดค๋ ์ฝ๋๋ค์ ์๊ฐํ๋ฉด App component์์ status ๋ฅผ ์์๋ก ๋ค ์ ์์ ๊ฑฐ ๊ฐ๋ค.๐คญ
Custom Hooks
Last updated
Was this helpful?