JOYLOG

[React JS] proxy, concurrently, redux, hook ๊ฐœ๋… ์ •๋ฆฌ ๋ณธ๋ฌธ

D E V E L O P E R ๐Ÿ’ป/Front-end

[React JS] proxy, concurrently, redux, hook ๊ฐœ๋… ์ •๋ฆฌ

์ฃผํž 2021. 7. 10. 12:08

CORS ์ด์Šˆ

  • Cross-Origin Resource Sharing (๋ณด์•ˆ์„ ์œ„ํ•œ ์ •์ฑ…)
  • ์›์ธ : request ๋ณด๋‚ด๋Š” client ์™€ response ๋ณด๋‚ด๋Š” server๊ฐ€ ๋‹ค๋ฅธ ํฌํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด, CORS ์ •์ฑ…์— ๋ง‰ํ˜€์ง
  • ํ•ด๊ฒฐ : ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ, ๊ทธ ์ค‘ ํ•˜๋‚˜๋Š” Proxy ์‚ฌ์šฉ

(https://create-react-app.dev/docs/proxying-api-requests-in-development#configuring-the-proxy-manually)

 

Proxy?

: ์œ ์ €์™€ ์ธํ„ฐ๋„ท ์‚ฌ์ด์— Proxy Server๊ฐ€ ์žˆ๋‹ค.

: IP๋ฅผ Proxy Server์—์„œ ์ž„์˜๋กœ ๋ฐ”๊ฟ”, ์ธํ„ฐ๋„ท์—์„œ๋Š” ์ ‘๊ทผํ•˜๋Š” ์‚ฌ๋žŒ์˜ IP๋ฅผ ๋ชจ๋ฅด๊ฒŒ ๋œ๋‹ค. ๋ฐ์ดํ„ฐ ๋˜ํ•œ ์ž„์˜๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

  1. ๋ฐฉํ™”๋ฒฝ ๊ธฐ๋Šฅ 2. ์›น ํ•„ํ„ฐ ๊ธฐ๋Šฅ 3. ์บ์‰ฌ ๋ฐ์ดํ„ฐ, ๊ณต์œ  ๋ฐ์ดํ„ฐ ์ œ๊ณต ๊ธฐ๋Šฅ

 

Proxy Server ์‚ฌ์šฉ ์ด์œ 

  1. ์ธํ„ฐ๋„ท ์‚ฌ์šฉ ์ œ์–ด (ํšŒ์‚ฌ, ๊ฐ€์ • ๋“ฑ ....)
  2. ์บ์‰ฌ๋ฅผ ์ด์šฉํ•ด ๋” ๋น ๋ฅธ ์ธํ„ฐ๋„ท ์ด์šฉ ์ œ๊ณต
  3. ๋ณด์•ˆ
  4. ์ด์šฉ ์ œํ•œ๋œ ์‚ฌ์ดํŠธ ์ ‘๊ทผ ๊ฐ€๋Šฅ

 


concurrently ํ”„๋ก ํŠธ, ๋ฐฑ์„œ๋ฒ„ ํ•œ ๋ฒˆ์— ํ‚ค๊ธฐ

concurrently ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ™œ์šฉ

npm install concurrently --save

 


React JS๋ฅผ ์œ„ํ•œ CSS Framework ์ข…๋ฅ˜

: ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ์— ์ดˆ์ ์„ ๋งž์ถ”๊ธฐ ์œ„ํ•ด CSS framework ๋ฅผ ์‚ฌ์šฉ

  1. Material UI 2. React Bootstrap 3. Semantic UI 4. Ant Design 5. Materialize ...

https://ant.design/

 

Ant Design - The world's second most popular React UI framework

 

ant.design

 

 


Redux?

๋จผ์ € props์™€ state์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ํ•„์š”.

 

Props

: Properties์˜ ์ค„์ž„๋ง.

: ์ปดํฌ๋„ŒํŠธ ๊ฐ„์— ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋กœ ๊ฐ’์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Œ.

: ์ด ๊ฐ’์€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ๋ณ€ํ•  ์ˆ˜ ์—†์Œ. (immutable)

 

State

: ์ปดํฌ๋„ŒํŠธ์˜ ๊ฐ’์ด ๋ฐ”๋€” ์ˆ˜ ์žˆ์œผ๋ฉฐ (mutable), state๊ฐ€ ๋ณ€ํ•˜๋ฉด ๋‹ค์‹œ ๋žœ๋”๋ง์ด ๋œ๋‹ค. (re-render)

: props์™€ ๋‹ค๋ฅด๊ฒŒ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

state = {message :'', attachFile : undefined, openMenu : false,};

 

Redux

: State๋ฅผ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ํˆด, ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

 

Redux ๋ฐ์ดํ„ฐ Flow

React Component -> (Dispatch) -> Action -> REDUCER -> Store -> (Subscribe) -> ๋‹ค์‹œ React Component ...

ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ํ๋ฅธ๋‹ค. (strict undirectional data flow)

 

  • Action : ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚ฌ๋Š”์ง€ ์„ค๋ช…ํ•˜๋Š” ๊ฐ์ฒด
  • {type : 'LIKE\_ARTICLE', articeldId : 42} // 42๋ฒˆ ๊ฒŒ์‹œ๊ธ€์„ ์ข‹์•„์š”

 

  • Reducer : Action์„ ํ†ตํ•ด ์ผ์–ด๋‚œ, state์˜ ๋ณ€ํ™”๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๊ณณ. ์ด์ „ State์™€ action object ๋ฐ›์€ ํ›„์˜ next state์„ return
  • (previousState, action) => nextState

 

  • Store :์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ state์„ ๊ฐ์‹ธ์ฃผ๋Š” ์—ญํ• . store ์•ˆ์—๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ method๊ฐ€ ์žˆ์–ด, ์ด๋ฅผ ์ด์šฉํ•ด state๋ฅผ ๊ด€๋ฆฌ.

 


Redux ์‚ฌ์šฉ์„ ์œ„ํ•œ Dependeny ๋‹ค์šด

  • redux
  • react-redux
  • redux-promis
  • redux-thunk
npm install redux react-redux redux-promise redux-thunk --save (client์—์„œ ์ง„ํ–‰)

 

Redux middleware ( redux-thunk, redux-promise)

: Redux๋ฅผ ์ž˜ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋•๋Š”๋‹ค.

Redux Store์—์„œ state์„ ๊ด€๋ฆฌํ•˜๋Š”๋ฐ, dispatch๋ฅผ ํ†ตํ•ด Action์—์„œ ๋ณ€๊ฒฝ์‹œํ‚จ๋‹ค. ์ด Action์€ ๊ฐ์ฒดํ˜•์‹์ด์—ฌ์•ผ store๊ฐ€ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ. ํ•˜์ง€๋งŒ Store์—์„œ ํ•ญ์ƒ ๊ฐ์ฒด ํ˜•์‹์˜ Action์„ ๋ฐ›๋Š” ๊ฒƒ์ด ์•„๋‹Œ, Promise, Function ํ˜•ํƒœ๋กœ ๋ฐ›์„ ๋•Œ๋„ ์žˆ๋‹ค.

  • redux-thunk : Dispatch์—๊ฒŒ Function์„ ๋ฐ›๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค์คŒ.
  • redux-promise : Dispatch์—๊ฒŒ Promise๋ฅผ ๋ฐ›๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค์คŒ.

 

Redux extension

https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd/related?hl=ko

 

Combine Reducer

: ์—ฌ๋Ÿฌ๊ฐ€์ง€ state์ด ์žˆ์œผ๋ฉด, ๋˜ ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ reducer๊ฐ€ ์žˆ๋Š”๋ฐ, combine reducer์€ root reducer์—์„œ ์ด๋“ค์„ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์คŒ.

(Combine Reducer => Root Reducer)

 

 


React Component์™€ Hook

 

Class Component (ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ)

: Provide more features, longer code, more complex code, slower performance

: ๋” ๋งŽ์€ ๊ธฐ๋Šฅ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

: ๊ธธ๊ณ  ๋ณต์žกํ•œ ์ฝ”๋“œ

: ์„ฑ๋Šฅ์ ์œผ๋กœ ๋Š๋ฆฐ ํผํฌ๋จผ์Šค

Functional Component (ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ)

: Provide less features, shorter code, simpler code, faster performance

: ์ ์€ ๊ธฐ๋Šฅ ์‚ฌ์šฉ๊ฐ€๋Šฅ

: ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ

: ์„ฑ๋Šฅ์ ์œผ๋กœ ๋น ๋ฅธ ํผํฌ๋จผ์Šค

 

Hook

Hook ์„ ํ†ตํ•ด functional component์—์„œ๋„ ๋น ๋ฅธ ํผํฌ๋จผ์Šค์™€ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ(state, lifecycle...) ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ.

Comments