[Week9/mission] Redux Toolkit / Zustand#51
Merged
leetaesk merged 8 commits intojackson/mainfrom Jun 19, 2025
Merged
Conversation
duwlsssss
approved these changes
May 30, 2025
Comment on lines
+23
to
+25
| {cartItems.map((item) => ( | ||
| <CartItem item={item} /> | ||
| ))} |
Contributor
There was a problem hiding this comment.
CartItem 컴포넌트에 key 필요합니다!
Comment on lines
+39
to
+40
| state.onConfirm = action.payload.onConfirm; | ||
| state.onCancel = action.payload.onCancel; |
Contributor
There was a problem hiding this comment.
노션에 이슈 확인했어요.
모달에 연결되야 하는 핸들러는 상태 말고
Middleware를 활용해 openModal, closeModal action에 연결해 두는 건 어떨까요??
제가 해 본 코드 공유드립니다!
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import { Middleware } from '@reduxjs/toolkit';
interface ModalState {
isOpen: boolean;
message: string;
confirmText?: string;
cancelText?: string;
modalType: string; // 모달 타입 식별
}
const initialState: ModalState = {
isOpen: false,
message: '',
confirmText: '예',
cancelText: '아니오',
modalType: '',
};
const modalSlice = createSlice({
name: 'modal',
initialState,
reducers: {
openModal: (
state,
action: PayloadAction<{
message: string;
confirmText?: string;
cancelText?: string;
modalType: string;
}>,
) => {
// 열기
state.isOpen = true;
// 모달설정
state.message = action.payload.message;
state.confirmText = action.payload.confirmText ?? '예';
state.cancelText = action.payload.cancelText ?? '아니오';
state.modalType = action.payload.modalType;
},
closeModal: (state) => {
// 닫기
state.isOpen = false;
},
},
});
// 모달에 핸들러 추가
const modalHandlers = new Map<
string,
{ onConfirm?: () => void; onCancel?: () => void }
>();
export const registerModalHandler = (
type: string,
handlers: { onConfirm?: () => void; onCancel?: () => void },
) => {
modalHandlers.set(type, handlers);
};
export const getModalHandler = (type: string) => {
return modalHandlers.get(type);
};
export const modalMiddleware: Middleware =
(store) => (next) => (action) => {
if ((action as PayloadAction).type === 'modal/openModal') {
const handler = modalHandlers.get(store.getState().modal.modalType);
handler?.onConfirm?.();
}
if ((action as PayloadAction).type === 'modal/closeModal') {
const handler = modalHandlers.get(store.getState().modal.modalType);
handler?.onCancel?.();
}
return next(action);
};
export const { openModal, closeModal } = modalSlice.actions;
export default modalSlice.reducer;그리고 모달 컴포넌트에서
export default function Modal() {
const dispatch = useAppDispatch();
const { isOpen, message, modalType } = useAppSelector((state) => state.modal);
// 핸들러 가져오기
const handler = modalType ? getModalHandler(modalType) : null;
if (!isOpen) return null;
const handleConfirm = () => {
handler?.onConfirm?.();
};
const handleCancel = () => {
if (handler?.onCancel) handler.onCancel();
else dispatch(closeModal());
};
// ...
}모달 사용할 때는
function CardCover({ cart }: CardCoverProps) {
// ...
// 모달에 핸들러 연결해두기
registerModalHandler('navigatrCartConfirm', {
onConfirm: () => {
dispatch(closeModal());
navigate(ROUTES.CART);
},
});
const handleClick = () => {
dispatch(addItem(cart));
dispatch(
openModal({
modalType: 'navigatrCartConfirm',
message: '장바구니로 이동하시겠습니까?',
}),
);
};
// ...
}
S-Gihun
approved these changes
May 30, 2025
starvingorange
approved these changes
Jun 18, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
✏️ 작업 내용
#️⃣ 연관된 이슈
#50
📷 작업 결과
week9-mission.mp4
💡 함께 공유하고 싶은 부분
모달을 슬라이스로 관리하는 부분에서 이슈가 좀 있었는데 노션에 정리해놨습니다. 해결 가능하다면 내일 스터디전까지 해갈게요
🤔 질문
✅ 워크북 체크리스트
✅ 컨벤션 체크리스트