Skip to content

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป Redux๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ API์„œ๋ฒ„์™€ ํ†ต์‹ ํ•ด์„œ ์ž‘๋™ํ•˜๋Š” ๋Œ“๊ธ€ CRUD ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•œ ํ”„๋กœ์ ํŠธ์ž…๋‹ˆ๋‹ค.

Notifications You must be signed in to change notification settings

hyejineee/redux-comment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

26 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Redux ๋Œ“๊ธ€ CRUD

์›ํ‹ฐ๋“œ ํ”„๋ฆฌ์˜จ๋ณด๋”ฉ ์ธํ„ด์‹ญ์—์„œ 4์ฃผ์ฐจ ๊ณผ์ œ๋กœ API ์„œ๋ฒ„์™€ ํ†ต์‹ ํ•ด์„œ ์ž‘๋™ํ•˜๋Š” ๋Œ“๊ธ€ ํ”„๋กœ์ ํŠธ๋ฅผ Redux๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

๋ฐฐํฌ ์‚ฌ์ดํŠธ์— ์ ‘์†ํ•˜๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ API ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•ด ์ฃผ์„ธ์š”
๐Ÿ”— ๋ฐฐํฌ ๋งํฌ

๋ชฉ์ฐจ

์š”๊ตฌ ์‚ฌํ•ญ

  • ๊ตฌํ˜„ ์‚ฌํ•ญ

    • ๋Œ“๊ธ€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ, ์ž‘์„ฑ, ์ˆ˜์ •, ์‚ญ์ œ๊ฐ€ ๋™์ž‘ํ•˜๋„๋ก ๊ธฐ๋Šฅ ๊ตฌํ˜„
    • ํŽ˜์ด์ง€๋„ค์ด์…˜
    • ๋Œ“๊ธ€ ์ž‘์„ฑ, ์ˆ˜์ •, ์‚ญ์ œ ํ›„ ๋™์ž‘
      • ๋Œ“๊ธ€ ์ž‘์„ฑํ•˜๊ณ  ๋‚œ ๋’ค: ๋‹ค๋ฅธ ํŽ˜์ด์ง€์— ์œ„์น˜ํ•˜๊ณ  ์žˆ์—ˆ๋”๋ผ๋„ 1ํŽ˜์ด์ง€๋กœ ์ด๋™, ์ž…๋ ฅ ํผ ์ดˆ๊ธฐํ™”
      • ๋Œ“๊ธ€ ์ˆ˜์ •ํ•˜๊ณ  ๋‚œ ๋’ค: ํ˜„์žฌ ๋ณด๊ณ ์žˆ๋Š” ํŽ˜์ด์ง€ ์œ ์ง€, ์ž…๋ ฅ ํผ ์ดˆ๊ธฐํ™”
      • ์‚ญ์ œํ•˜๊ณ  ๋‚œ ๋’ค: 1ํŽ˜์ด์ง€๋กœ ์ด๋™
  • ๊ตฌํ˜„ ์กฐ๊ฑด

    • Redux ํ™˜๊ฒฝ์„ค์ •์€ ์ž์œ ๋กญ๊ฒŒ ์ง„ํ–‰
      • Redux-saga or Redux-thunk ์ž์œ ๋กญ๊ฒŒ ์„ ํƒ ๊ฐ€๋Šฅ
      • ๋ฏธ๋“ค์›จ์–ด ์‚ฌ์šฉ์•ˆํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ
    • Redux logger, Redux-Devtools ์„ค์ • ํ•„์ˆ˜
    • Redux๋ฅผ ์ด์šฉํ•œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ํ•„์ˆ˜
  • ๊ฐœ๋ฐœ ์กฐ๊ฑด ๋ฐ ํ™˜๊ฒฝ

    • ์–ธ์–ด : JavaScript / TypeScript
    • ํ•„์ˆ˜ ๊ธฐ์ˆ : React, Redux, Redux-Logger, Redux-Devtools
    • ์„ ํƒ ๊ธฐ์ˆ :
      • Redux ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(Toolkit, middleware ๋“ฑ)
      • ์Šคํƒ€์ผ ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(styled-components, emotion, UI kit ๋“ฑ)
      • HTTP Client(axios ๋“ฑ)
    • ์œ„์— ๊ธฐ์žฌ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์™ธ ์‚ฌ์šฉ ๋ถˆ๊ฐ€

๊ตฌํ˜„ ๋‚ด์šฉ

  • redux-toolkit์— ๋‚ด์žฅ๋˜์–ด ์žˆ๋Š” thunk api๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ redux์—์„œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

  • axios๋ฅผ ๊ฐ์‹ผ HttpClient ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , HttpClient๋ฅผ ์ฃผ์ž…๋ฐ›์•„ ํ†ต์‹  ๊ธฐ๋Šฅ์„ ์ถ”์ƒํ™”ํ•œ repository ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. action์˜ payload๋กœ repository๋ฅผ ๊ฐ™์ด ๋ฐ›์•„ redux์—์„œ๋Š” ๋„คํŠธ์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ง€, ๋””๋น„๋ฅผ ์‚ฌ์šฉํ•˜๋Š”์ง€ ๋ชจ๋ฅผ ์ˆ˜ ์žˆ๋„๋ก ์˜์กด์„ฑ์„ ๋ถ„๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.

export const fetchCommentsAction = createAsyncThunk(
`${name}/fetchComments`,
async ({ repository }: ThunkPayloadType<undefined>, thunkAPI: any) => {
try {
const comments = await repository.fetchComments();
thunkAPI.dispatch(updateTotalComment(comments.length));
return comments;
} catch (e) {
return thunkAPI.rejectWithValue((e as AxiosError).message);
}
},
);

export interface ICommentRepository {
fetchCommentsPage: (page: number) => Promise<CommentType[]>;
fetchComments: () => Promise<CommentType[]>;
fetchComment: (id: number) => Promise<CommentType>;
createComment: (newComment: CommentType) => void;
updateComment: (id: number, updated: CommentType) => void;
deleteComment: (id: number) => void;
}

๋Œ“๊ธ€ ํŽ˜์ด์ง€๋„ค์ด์…˜
๋Œ“๊ธ€ ๋“ฑ๋ก
๋Œ“๊ธ€ ์ˆ˜์ •
๋Œ“๊ธ€ ์‚ญ์ œ

ํšŒ๊ณ 

1. ์˜๋„ํ•œ ๊ฒฐ๊ณผ๋Š” ๋ฌด์—‡์ด์—ˆ๋Š”๊ฐ€? (์ดˆ๊ธฐ ๋ชฉํ‘œ)

  • ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉํ–ˆ๋˜ repository ํŒจํ„ด์„ ํ”„๋กœ์ ํŠธ์— ์ ์šฉํ•˜๋Š”๋ฐ ์ง‘์ค‘ํ•˜์—ฌ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

2. ์‹ค์ œ ์–ด๋–ค ์ผ๋“ค์ด ์ผ์–ด๋‚ฌ๋Š”๊ฐ€? (ํ˜„์‹ค)

  • redux ์ƒํƒœ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ repository ํŒจํ„ด์„ ์ ‘๋ชฉ์‹œํ‚ค๋Š”๋ฐ ์–ด๋ ค์›€์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. redux์˜ reducer๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜์ด๊ณ  repository๋Š” ํด๋ž˜์Šค ํ˜•ํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ป๊ฒŒ reducer์—์„œ repository๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ? ํ•˜๋Š”๊ฒŒ ์ œ์ผ ํฐ ๊ณ ๋ฏผ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ์กด์— ์˜์กด์„ฑ ์ฃผ์ž…์„ ์–ด๋–ป๊ฒŒ ํ–ˆ๋Š”๊ฐ€?๋ฅผ ๋‹ค์‹œ ์ƒ๊ฐํ•ด ๋ณด๊ฒŒ ๋˜์—ˆ๊ณ , ๊ธฐ์กด์— ํ–ˆ๋˜ ๋ฐฉ๋ฒ•๋„ ์ƒ์„ฑ์ž ๋ฉ”์†Œ๋“œ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ์ฃผ์ž…ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๊นจ๋‹ซ๊ณ  thunk action์— payload๋กœ repository๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

3. ์ง€์†, ๊ฐœ์„  ํ˜น์€ ํฌ๊ธฐํ•  ๊ฒƒ๋“ค์€ ๋ฌด์—‡์ธ๊ฐ€? ๋ฐฐ์šด ๊ฒƒ๋“ค์€ ๋ฌด์—‡์ธ๊ฐ€? (๋ชฉ์ )

  • ๊ธฐ์กด์— ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ๊ฐ์ฒด ์ง€ํ–ฅ์„ ํ•˜๋ฉด์„œ ํ‹€์— ๋ฐ•ํ˜”๋˜ ์‚ฌ๊ณ ๋ฅผ ๊นจ๋Š” ์‹œ๊ฐ„์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํด๋ž˜์Šค์ธ๊ฐ€ ํ•จ์ˆ˜์ธ๊ฐ€๊ฐ€ ์ค‘์š”ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ธฐ์กด์— ๋™์ž‘ํ–ˆ๋˜ ๊ฒƒ๋“ค์ด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ๋Œ์•„๊ฐ”๋Š”์ง€ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ซ๋Š” ์‹œ๊ฐ„์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๊ธฐ์ˆ  ์Šคํƒ

์‹คํ–‰๋ฐฉ๋ฒ•

  1. ํ”„๋กœ์ ํŠธ๋ฅผ ํด๋ก ํ•ฉ๋‹ˆ๋‹ค.
git clone https://github.com/hyejineee/redux-comment.git
  1. ํด๋ก ํ•œ ํ”„๋กœ์ ํŠธ ๋‚ด๋ถ€๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
cd redux-comment
  1. ์˜์กด ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
yarn
  1. ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
# api ์„œ๋ฒ„ ์‹คํ–‰ 
yarn api

# ํด๋ผ์ด์–ธํŠธ ์‹คํ–‰
yarn dev 

About

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป Redux๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ API์„œ๋ฒ„์™€ ํ†ต์‹ ํ•ด์„œ ์ž‘๋™ํ•˜๋Š” ๋Œ“๊ธ€ CRUD ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•œ ํ”„๋กœ์ ํŠธ์ž…๋‹ˆ๋‹ค.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published