Redux ์ ์šฉ

๊น€๋ด‰์„ญยท2023๋…„ 2์›” 19์ผ
0

TieDesign ํ”„๋กœ์ ํŠธ

๋ชฉ๋ก ๋ณด๊ธฐ
2/6
post-thumbnail

๐Ÿƒ๐Ÿปโ€โ™‚๏ธ ๊ธฐ์กด ํ”„๋กœ์ ํŠธ๋Š” ์ƒํƒœ๊ด€๋ฆฌ ๋„๊ตฌ ์‚ฌ์šฉ ์—†์ด ์ปดํฌ๋„ŒํŠธ์— Props๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์‚ฌ์šฉํ–ˆ๋‹ค.

<Editor
   setModal={setModal}
   selected={selected}
   setSelected={setSelected}
   elStage={elStage}
   initialItem={initialItem}
   initialOnChange={setInitialItem}
   />
----------
function Editor({ initialItem, elStage, initialOnChange, setSelected, selected, setModal }) {...}

๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ปดํฌ๋„ŒํŠธ๋กœ Props ์ง€์˜ฅ์„ ๊ฒช์—ˆ๋‹ค.

ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด Redux๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

์•„๋ž˜๋Š” ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ts ํŒŒ์ผ์ด๋‹ค.

import { createSlice } from "@reduxjs/toolkit";

export interface PositionType {
  isDragging: boolean;
  width: number;
  height: number;
  x: number;
  y: number;
}

export interface DesignType {
  label: string;
  preview: string;
  positions: PositionType;
}

export interface InitialItemType {
  color: string;
  designs: DesignType[];
}

const initialState: InitialItemType = {
  color: "white",
  designs: [
    {
      label: "main_logo",
      preview: "",
      positions: {
        isDragging: false,
        width: 144,
        height: 139,
        x: 0,
        y: 0,
      },
    },
    {
      label: "setOne_logo",
      preview: "",
      positions: {
        isDragging: false,
        width: 144,
        height: 139,
        x: 0,
        y: 0,
      },
    },
    {
      label: "setTwo_logo",
      preview: "",
      positions: {
        isDragging: false,
        width: 144,
        height: 139,
        x: 0,
        y: 0,
      },
    },
  ],
};

export const initialSlice = createSlice({
  name: "initialItem",
  initialState,
  reducers: {
    dragstart: (state) => {
      state.designs[0].positions.isDragging = true;
    },
    dragend: (state, action) => {
      state.designs[0].positions = action.payload;
    },
    change: (state, action) => {
      state.designs[0].positions = action.payload;
    },
    changecolor: (state, action) => {
      state.color = action.payload;
    },
    changedesign: (state, action) => {
      state.designs = action.payload;
    },
    changepreview: (state) => {
      state.designs[0].positions.isDragging = true;
    },
  },
});

export const { dragstart, dragend, change, changecolor, changedesign } = initialSlice.actions;

export default initialSlice.reducer;

redux๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ถ”๊ฐ€๋กœ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ๋„ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
redux์˜ dispatch๋Š” ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜๋˜ ์ฝ”๋“œ๋ฅผ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด์คฌ๋‹ค.

onDragStart={() => {
          onChange({
            ...data,
            designs: data.designs.map((arg) => {
              return arg.label === "main_logo"
                ? {
                    ...data.designs[0],
                    positions: {
                      ...data.designs[0].positions,
                      isDragging: true,
                    },
                  }
                : arg;
            }),
          });
        }}
        
----------------------------------------------------
 onDragStart={() => {
          dispatch(dragstart());
        }}

์œ„ ์ฝ”๋“œ ์ฒ˜๋Ÿผ boolean ๊ฐ’ ํ•˜๋‚˜๋ฅผ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•œ 16์ค„์˜ ์ฝ”๋“œ๋ฅผ 3์ค„์˜ ์ฝ”๋“œ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

Redux ์‚ฌ์šฉ์„ ํ†ตํ•ด ์–ป์€ ์ด์ 
1. props ์ง€์˜ฅ ํƒˆ์ถœ
2. redux์—์„œ ๋ฐ์ดํ„ฐ์™€ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์„ ์œ„ํ•œ ํ•จ์ˆ˜ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•˜์—ฌ ์ฝ”๋“œ ํšจ์œจ, ๊ฐ€๋…์„ฑ ์ฆ๊ฐ€

profile
ํ”„๋ก ํŠธ์—”๋“œ ์›น&์•ฑ ๊ฐœ๋ฐœ

0๊ฐœ์˜ ๋Œ“๊ธ€