๐ React Native ๋ฆฌ์กํธ ๋ค์ดํฐ๋ธ - ์ด๊ฒ์ ๊ฒ ๋ฉ๋ชจ
2023-12-05. 14:57
Mobile Programming Test ๊ธ ๊ณ์น
๐ซ ์ค์ฒฉ๋ ScrollView ๋ฐฉํฅ์ ์ ์ฝ
@ TODO : 212p
๐ซ Javascript, Typescript
ํจ์ ๊ตฌํ ์ฝ๋ Func(): A
์์ : A
๋ ํจ์๊ฐ A๋ฅผ ๋ฐํํ๋ค๋ ์๋ฏธ.
Pick ํ์
โ ์ ๋ค๋ฆญ ํ์
, ๋์ ํ์
์ ์ ์ฒด ์์ฑ ์ค ํ์ํ ์์ฑ๋ง ์ ํํ์ฌ ๋ฐํ
1
type NewType = Pick<SomeType, 'SomePropertyA' | 'SomePropertyB'>
๊ฐ์ฒด๊ฐ ์ ๊ณตํ๋ ์์ฑ์ ์๊ธฐ์ํด์, console.log(Objects.keys(๊ฐ์ฒด)) ์ฝ๋๋ฅผ ์ด์ฉํ ์ ์๋ค.
๐ซ Fetch
GET/POST/PUT/DELETE๋ฅผ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก, Javascript ์์ง์์ ๊ธฐ๋ณธ ์ ๊ณตํ๋ API์ด๋ค.
blob, json, text ๊ฐ์ ๋ฉ์๋๊ฐ ์๋ Response ํ์ ๊ฐ์ฒด๋ฅผ Promise ๋ฐฉ์์ผ๋ก ์ป์ ์ ์๊ฒ ํด์ค๋ค.
Promise ๊ฐ์ฒด๋ then ๋ฉ์๋๋ฅผ ํตํด ์ค์ ๋ฐ์ดํฐ๋ฅผ ์ป์ด์ผ ํ๋ค.
then ๋ฉ์๋๋ ๋ ๋ค๋ฅธ Promise ๊ฐ์ฒด๋ ์ด๋ค ๊ฐ์ ๋ฐํํ ์ ์๋ค.
then์ ์ฐ๋ฌ์ ํธ์ถํ์ฌ ์ฌ์ฉํ ์ ์๋๋ฐ, ์ด๋ฅผ then-์ฒด์ธ์ด๋ผ ํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// ์ ์
function fetch(input: RequestInfo, init?:RequestInit): Promise<Response>
interface Response
{
blob(): Promise<Blob>;
json(): Promise<any>;
text(): Promise<string>;
}
// ์ฌ์ฉ
fetch('RequestInfo Like URL')
fetch('RequestInfo Like URL')
.then((res) => res.json())
.then((blabla) => someMethod(blabla))
.catch((error: Error) => console.log(error.message))
๐ซ ํค์๋
ActivityIndicator ์ฝ์ด ์ปดํฌ๋ํธ
โ ํ์ ํ๋ ์์ด์ฝ, react-native ํจํค์ง
1
2
3
4
5
export default function Timer()
{
const [loading, setLoading] = useState(false)
return ( { loading && (<ActivityIndicator>) } )
}
๐ซ JSX์์ if๋ฌธ์ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ
@ U ์ค๊ฐ๊ณ ์ฌ ์ถ์ :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
export default function App()
{
const isLoading = true
if (isLoading)
{
return
{
<SafeAreaView>
<Text>Loading...</Text>
</SafeAreaView>
}
}
return
{
<SafeAreaView>
<Text>Hello JSX World !</Text>
</SafeAreaView>
}
}
1
2
3
4
5
6
7
8
9
10
11
{ /* ๋จ์ถํ๊ฐ Short Circuit Evaluation */ }
export default function App()
{
const isLoading = true
<SafeAreaView>
{ /* A && B : undefined */ }
{ /* JSX Parser, undefined or null ๋ฌด์ */ }
{isLoading && <Text>Loading...</Text>}
{!isLoading && <Text>Hello JSX World !</Text>}
</SafeAreaView>
}
1
2
3
4
5
6
7
8
9
{ /* ๋จ์ถํ๊ฐ Short Circuit Evaluation */ }
export default function App()
{
const isLoading = true
const children = isLoading ?
(<Text>Hello JSX World !</Text>) :
(<Text>Loading...</Text>)
return <SafeAreaView>{children}</SafeAreaView>
}
๐ซ ํจ์์ปดํฌ๋ํธ ์์ฑ ์ ์ ๋ฐ ์ ๋ฌ ๋ฐฉ๋ฒ
@ U ์ค๊ฐ๊ณ ์ฌ ์ถ์ :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
{ /* XML(Markup Language) : Attribute, TS(Programming Language) : Property */ }
<Person name = "Jack" age = {22}/>
{ /* ์์ชฝ ์ค๊ดํธ : ๊ฐ์ฒด ์์ฑ, ๋ฐ๊นฅ์ชฝ ์ค๊ดํธ : JSX ๊ตฌ๋ฌธ */ }
<Person person ={{name: 'Jack', age: 32}}/>
{ /* @ */ }
{ /* ๊ฐ์ DOM ๊ฐ์ฒด = createElement(์ปดํฌ๋ํธ ์ด๋ฆ or ๋ฌธ์์ด, `์์ฑ ๊ฐ์ฒด`, ์์ ์ปดํฌ๋ํธ) */ }
{ /* or */ }
{ /* ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- */ }
export type IPerson
{
id: string
createdDate : Date
counts:
{
comment: number
retweet: number
}
}
export const createRandomPerson = (): IPerson =>
{
return
{
id: ~,
...
}
}
{ /* ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- */ }
import React from 'react'
{ /* import type : TSโJS ์ปดํ์ผ ๋๋ง ํ์ํ 'ํ์
', ๋ฐ๋ฉด ํด๋์ค๋ ๋จ์ */ }
{ /* FC : Function Component */ }
import type {FC} from 'react'
import * as D from './~'
export type PersonProps =
{
person: D.IPerson
}
const Person: FC<PersonProps> = ({person}) =>
{
{ /* ๊ฐ์ฒด ๊ทธ๋๋ก ์ถ๋ ฅํ ์ ์์ด์, ๊ณต๋ฐฑ 2๊ฐ ๋ถ์ ๋ฌธ์์ด๋ก, Like .ToString */ }
return <Text>{JSON.stringify(person, null, 2)}</Text>
}
export default Person
{ /* ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- */ }
import Person from './~'
import * as D from './~'
const person = D.createRandomPerson()
export default function App()
{
return <ArrowComponent person = {person} />
}
๐ซ ์์ฌ์ฐ์ฐ์
@ U ์ค๊ฐ๊ณ ์ฌ ์ถ์ :
ESNext JS์ TS, Rest Operator - ์์ฌ ์ฐ์ฐ์ ์ง์
1
2
3
4
5
6
7
8
9
10
11
12
let address: any
{
country: 'Korea',
city: 'Seoul',
address1: 'Gangnam-gu',
address2: '~',
address3: '~'
}
const {country, city, ...detail} = address
{ /* detail = { address1, address2, address3 } */ }
๐ซ ์์ ๋ณต์ฌ & ๊น์ ๋ณต์ฌ
@ U ๊ธฐ๋ง๊ณ ์ฌ ์ถ์ : TODO, ์ ๊ฐ ์ฐ์ฐ์
๐ซ React.Fragment ์ปดํฌ๋ํธ
@ U ์ค๊ฐ๊ณ ์ฌ ์ถ์ :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import React, {Fragment} from 'react'
{ /* JSX = XML, ๋ค์๊ฐ์ด ๋ถ๋ชจ ์ปดํฌ๋ํธ ์์ด๋ ์ฌ๋ฌ ์ปดํฌ๋ํธ๊ฐ ์ฌ ์ ์์ */ }
<SafeAreaView />
<View />
{ /* Fragment : ์ค์ฒด๊ฐ ์๋ ๊ฑด ์๋์ง๋ง, XML ๋ฌธ๋ฒ์ด ์๊ตฌํ๋ ๋ถ๋ชจ ์ปดํฌ๋ํธ ์ญํ ๋ก ๋์ํ๋๋ก */ }
<Fragment>
<SafeAreaView />
<View />
</Fragment>
{ /* ๋จ์ถ ๊ตฌ๋ฌธ */ }
<>
<SafeAreaView />
<View />
</>
๐ซ ํ์ ์คํฌ๋ฆฝํธ์ ๊ต์งํฉ ํ์ ๊ตฌ๋ฌธ
@ U ์ค๊ฐ๊ณ ์ฌ ์ถ์ :
TS, Algebraic Data Type - ADT - ๋์ ๋ฐ์ดํฐ ํ์ ์ง์
ํฉ์งํฉ ํ์
โ type A_OR_B = A | B
๊ต์งํฉ ํ์
โ type A_AND_B = A & B
๐ซ useStyle ์ปค์คํ ํ ๋ง๋ค๊ธฐ
@ U ๊ธฐ๋ง๊ณ ์ฌ ์ถ์ : TODO, useStyle ์ปค์คํ ํ ๋ง๋ค๊ธฐ (6์ฅ), 400p