Untitled

shortid

๋ฐฐ์—ด๋ฐ์ดํ„ฐ(๋”๋ฏธ๋ฐ์ดํ„ฐ)์— ์•„์ด๋””๋ฅผ ๋„ฃ์–ด์ค„๋•Œ ์ •๋ง ๋žœ๋คํ•œ ๊ฐ’์„ ์ฐพ์•„์ค˜์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋“ค์ด ์žˆ๋Š”๋ฐ ์ด๋•Œ ์œ ์šฉํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ shortid์ด๋‹ค

yarn add shortid
import shortId from "shortid";

id: shortId.generate()

์œ„์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ํŒŒ์ผ์—์„œ ์ž„ํฌํŠธํ•œ๋’ค ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ์— ํ˜ธ์ถœํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๋”๋ฏธ๋ฐ์ดํ„ฐ์— ์‚ฌ์šฉํ•˜๊ธฐ ์ข‹์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋˜ faker์ด๋‹ค.

๊ฐ์ข… ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋‹ค.

immer

๋งŒ์•ฝ ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒํƒœ๊ฐ€ ์žˆ๋‹ค๋ฉด

mainPosts: [
    {
      id: 1,
      User: {
        id: 1,
        nickname: "Conrad",
      },
      content: "์ฒซ๋ฒˆ์งธ๊ฒŒ์‹œ๊ธ€ #sssss",
      Images: [
        {
          src:
            "https://media.vlpt.us/images/wooder2050/post/d2764478-dc72-4cc9-9128-f66bfb8b3aa3/reactintroduction.png",
        },
        {
          src:
            "https://media.vlpt.us/images/wooder2050/post/d2764478-dc72-4cc9-9128-f66bfb8b3aa3/reactintroduction.png",
        },
        {
          src:
            "https://media.vlpt.us/images/wooder2050/post/d2764478-dc72-4cc9-9128-f66bfb8b3aa3/reactintroduction.png",
        },
      ],
      Comments: [
        {
          User: {
            nickname: "nero",
          },
          content: "์™€์šฐ",
        },
        {
          User: {
            nickname: "hero",
          },
          content: "์˜ค์•„์•„์•„",
        },
      ],
    },
  ],

๋ถˆ๋ณ€์„ฑ์„ ์œ ์ง€ํ•ด์ฃผ๊ธฐ๊ฐ€ ์‰ฝ์ง€ ์•Š์€์ผ์ด๋‹ค....

์‹ค์ œ๋กœ ๋Œ“๊ธ€ํ•˜๋‚˜ ๋‹ค๋Š”๋ฐ ๋ถˆ๋ณ€์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜์˜ ์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค

 case ADD_COMMENT_SUCCESS: {
      //๋Œ“๊ธ€์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฒŒ์‹œ๊ธ€์„ ๋จผ์ € ์ฐพ๊ณ  ๊ทธ ์•ˆ์— ์ฝ”๋งจ์ธ ๋ฐฐ์—ด์— ์ ‘๊ทผํ•ด์„œ ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜๋Š”๋ฐ ๋ถˆ๋ณ€์„ฑ์„ ์œ ์ง€ํ•œ๋‹ค.
      const postIndex = state.mainPost.findIndex(
        (y) => y.id === action.data.postId
      );
      const post = state.mainPost[postIndex];
      const Comments = [dummyComment(action.data.content), ...post.Comments];
      const mainPosts = [...state.mainPosts];
      mainPosts[postIndex] = { ...post, Comments };
      return {
        ...state,
        addCommentLoading: false,
        addCommentDone: true,
        addCommentError: null,

        //mainComments: [dummyComment, ...state.mainComments], //์•ž์— ์ถ”๊ฐ€ํ•ด์•ผ ๊ฒŒ์‹œ๊ธ€์ด ์œ„์— ๋‚˜ํƒ€๋‚œ๋‹ค.
      };
    }

immer๋ฅผ ์“ฐ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 case ADD_COMMENT_SUCCESS: {
        const post = draft.mainPosts.find((v) => v.id === action.data.postId);
        post.Comments.unshift(dummyPost(action.data.content));
        draft.addCommentLoading = false;
        draft.addCommentDone = true;
        break;
      }

์šฐ์„  ์„ค์น˜

yarn add immer
import produce from "immer";

์•„๋ž˜๊ฐ€ ๊ธฐ๋ณธ ํ˜•์‹์ด๋‹ค.

export default function reducer(state = initialState, action) {
  return produce(state,draft=>{
    
  })
  switch (action.type) {

draft๋ฅผ ์Šคํ…Œ์ดํŠธ๋กœ ๋ณด๊ณ  ํŽธํ•˜๊ฒŒ ๋ฐ”๊พธ๋ฉด immer๊ฐ€ ์•Œ์•„์„œ ๋ฐ”๊ฟ”์ค€๋‹ค.

๋‹จ state๋ฅผ ๊ฑด๋“ค๋ฉด ์•ˆ๋˜๊ณ  draft๋งŒ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•œ๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋ฉด

 case ADD_POST_REQUEST:
        return {
          ...state,
          addPostLoading: true,
          addPostDone: false,
          addPostError: null,
        };
 case ADD_POST_REQUEST:
        return:
        draft.addPostLoading = true;
        draft.addPostDone = false;
        draft.addPostError = null;
        break;

Faker

๋”๋ฏธ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค๋•Œ ์กฐ๊ธˆ๋” ํ˜„์‹ค๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

๊น”์•„์ฃผ์‹œ๊ณ 

yarn add faker

์ž„ํฌํŠธํ•ด์ฃผ์†Œ

import faker from "faker";
initialState.mainPosts = initialState.mainPosts.concat( //concat์—๋Š” ํ•ญ์ƒ๋Œ€์ž…์„!
  Array(20)
    .fill()
    .map(() => ({
      id: shortId.generate(), //shortid ์‚ฌ์šฉ
      User: {
        id: shortId.generate(), //shortid ์‚ฌ์šฉ
        nickname: faker.name.findName(), //faker์‚ฌ์šฉ
      },
      content: faker.lorem.paragraph(),
      Images: [
        {
          src: faker.image.imageUrl(),
        },
      ],
      Comments: [
        {
          User: {
            id: shortid.generate(),
            nickname: faker.name.findName(),
          },
          content: faker.lorem.sentence(),
        },
      ],
    }))
);

์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•˜๊ฒŒ mainPosts20๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด๋ฒ„๋ ธ์ฃ ...

๋‚˜์ค‘์— ์„ฑ๋Šฅ ์ตœ์ ํ™”๊นŒ์ง€ ์ƒ๊ฐํ•ด๋ณธ๋‹ค๋ฉด ๋ช‡์ฒœ๊ฐœ์”ฉ ๋งŒ๋“ค์–ด์„œ ํ…Œ์ŠคํŠธํ•ด๋„ ์ข‹์Šต๋‹ˆ๋‹ค.

์ž์„ธํ•œ ๋‚ด์šฉ์ด๋‚˜ ์ข…๋ฅ˜๋Š” ๊นƒํ—ˆ๋ธŒ ๊ณต์‹๋ฌธ์„œ์— ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค.

๋ฆฌ๋•์Šคํˆดํ‚ท

๋ฆฌ๋•์ŠคํŒ€์—์„œ ๋งŒ๋“ ํˆด์ธ๋ฐ ํŽธํ•˜๊ฒŒ ๋ฆฌ๋•์Šค๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฝ”๋“œ๋Ÿ‰์„ ์กฐ๊ธˆ์”ฉ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

Last updated