ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฌธ์ œ

redux store

 

API ํ†ต์‹  ๊ฒฐ๊ณผ

ํ˜„์žฌ mainPage redux store์—๋Š” ํฌ๊ฒŒ ์ฝ˜ํ…์ธ ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” contents์™€ ์‚ฌ์šฉ์ž ๋ฐฉ์„ ๋ณด์—ฌ์ฃผ๋Š” roomList๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค.

 

redux-saga๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„์™€ API ํ†ต์‹ ์„ ํ•˜์—ฌ ๊ฐ๊ฐ์˜ ๋ฐฐ์—ด ๋‚ด์— ๋ฐ์ดํ„ฐ๋“ค์„ ์ ์žฌํ•ด์ฃผ๋Š” ๊ณผ์ •์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋Š”๋ฐ, contents์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๋“ค์€ ์„œ๋ฒ„์—์„œ ๋ฌธ์ œ ์—†์ด ๊ฐ€์ ธ์™€ ์ €์žฅ๋˜๋Š” ๋ฐ˜๋ฉด์—, roomList์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๋“ค์€ ์ €์žฅ๋˜์ง€ ์•Š์•˜๋‹ค.

 

์ฒ˜์Œ์—๋Š” ์„œ๋ฒ„์™€ API ํ†ต์‹ ํ•˜๋Š” ๊ณผ์ •์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ์ค„ ์•Œ๊ณ  Promise ํ›„์† ๋ฉ”์„œ๋“œ์ธ then ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ฝ˜์†”์„ ์ฐ์–ด๋ดค๋Š”๋ฐ, ๋ฌธ์ œ ์—†์ด roomList์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๋“ค๋„ ๊ฐ€์ ธ์™”๋‹ค.

 

๋ฌธ์ œํ•ด๊ฒฐ

ํ•œ์ฐธ์„ ํ•ด๋‹น Blocker์— ๋Œ€ํ•ด์„œ ๊ณ ๋ฏผํ•œ ๊ฒฐ๊ณผ, ๋ฆฌ์•กํŠธ ์ƒ์—์„œ ๋ถˆ๋ณ€์„ฑ์„ ์ง€์ผœ์ฃผ์ง€ ๋ชปํ•ด ๋ฐœ์ƒํ•œ ๋ฌธ์ œ์ž„์„ ํ™•์ธํ•˜์˜€๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ ์ƒ์—์„œ contents์™€ roomList์— ํ•ด๋‹นํ•˜๋Š” API ํ†ต์‹ ์„ ์ง„ํ–‰ํ•˜์˜€๊ณ ,

 useEffect(() => {
    dispatch(initializeMainForm()); // mainPage ์ดˆ๊ธฐํ™”

    if (sort === "friends") {
      batch(() => {
        dispatch(friendList());
        dispatch(friends());
      });
    } else {
      batch(() => {
        dispatch(content({ sort }))
        dispatch(rooms({ sort }));
      });
    }

    resetPageScroll();
  }, [dispatch, sort]);

 

 

redux-saga๋ฅผ ํ†ตํ•ด ๋ฐ›์•„ ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ action.payload์— ํ• ๋‹นํ•˜๊ณ  redux-store์—์„œ contents์™€ roomList ๋ฐฐ์—ด์— ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์ดˆ๊ธฐ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋‹ค.

const mainForm = handleActions(
  {
    [INITIALIZE_MAINFORM]: state => initialState,
    [CONTENT_SUCCESS]: ({ contents }, { payload: { message } }) => ({
      contents: contents.concat(message),
      contentsError: null,
    }),
    [CONTENT_FAILURE]: (state, { payload: error }) => ({
      ...state,
      contentsError: error,
    }),
    [ROOMS_SUCCESS]: ({ contents }, { payload: { message } }) => ({
      contents: contents.concat(message),
      contentsError: null,
    }),
    [ROOMS_FAILURE]: (state, { payload: error }) => ({
      ...state,
      contentsError: error,
    }),
  },
  initialState
);

 

contents์™€ roomList๋ฅผ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ์ƒ์œ„ ๊ฐ์ฒด_initialState์˜ ์ด์ „ ๊ฐ’์„ ๋ณต์‚ฌํ•˜์—ฌ ์ƒˆ๋กœ์šด ๊ฐ’์„ ํ• ๋‹นํ•˜์ง€ ์•Š์€ ์ฑ„, CONTENT_SUCCESS / ROOMS_SUCCESS ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๊ฐ์ฒด ๊ฐ’์„ ์žฌํ• ๋‹นํ•ด์ฃผ๋Š” ๋ฐ”๋žŒ์— ํ•ด๋‹น Blocker๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋˜ ๊ฒƒ์ด๋‹ค.

 

์ด์— ํ•ด๋‹น ์ฝ”๋“œ์˜ ๋ถˆ๋ณ€์„ฑ์„ ์ข€ ๋” ์•ˆ์ •์ ์ด๊ณ  ํŽธํ•˜๊ฒŒ ์ง€์ผœ์ฃผ๊ธฐ ์œ„ํ•ด immer ๋ชจ๋“ˆ์˜ produce๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌํŒฉํ† ๋งํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

const mainForm = handleActions(
  {
    [INITIALIZE_MAINFORM]: state => initialState,
    [CONTENT_SUCCESS]: (state, { payload: { message } }) =>
      produce(state, draft => {
        draft.contents = state.contents.concat(message);
        draft.contentsError = null;
      }),
    [CONTENT_FAILURE]: (state, { payload: error }) => ({
      ...state,
      contentsError: error,
    }),
    [ROOMS_SUCCESS]: (state, { payload: { message } }) =>
      produce(state, draft => {
        draft.contents = state.contents.concat(message);
        draft.contentsError = null;
      }),
    [ROOMS_FAILURE]: (state, { payload: error }) => ({
      ...state,
      contentsError: error,
    }),
  },
  initialState
);

 

๊ฒฐ๊ณผํ™”๋ฉด

contents ๋ผ๋Š” ์˜๋ฏธ๊ฐ€ ๋‹ค์†Œ ๋ถˆ๋ช…ํ™•ํ•œ ๋Š๋‚Œ์ด ๋“ค์–ด contentList๋กœ ํ‚ค ๊ฐ’์„ ์ˆ˜์ •ํ•˜์˜€๋‹ค

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ