๐๐ปโโ๏ธ ๊ธฐ์กด ํ๋ก์ ํธ๋ ์ํ๊ด๋ฆฌ ๋๊ตฌ ์ฌ์ฉ ์์ด ์ปดํฌ๋ํธ์ 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์์ ๋ฐ์ดํฐ์ ๋ฐ์ดํฐ ๋ณ๊ฒฝ์ ์ํ ํจ์ ๋ฐ๋ก ๊ด๋ฆฌํ์ฌ ์ฝ๋ ํจ์จ, ๊ฐ๋ ์ฑ ์ฆ๊ฐ