-
Notifications
You must be signed in to change notification settings - Fork 1
[Week9/mission] Redux Toolkit / Zustand #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| {cartItems.map((item) => ( | ||
| <CartItem item={item} /> | ||
| ))} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CartItem 컴포넌트에 key 필요합니다!
| state.onConfirm = action.payload.onConfirm; | ||
| state.onCancel = action.payload.onCancel; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
노션에 이슈 확인했어요.
모달에 연결되야 하는 핸들러는 상태 말고
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: '장바구니로 이동하시겠습니까?',
}),
);
};
// ...
}
✏️ 작업 내용
#️⃣ 연관된 이슈
#50
📷 작업 결과
week9-mission.mp4
💡 함께 공유하고 싶은 부분
모달을 슬라이스로 관리하는 부분에서 이슈가 좀 있었는데 노션에 정리해놨습니다. 해결 가능하다면 내일 스터디전까지 해갈게요
🤔 질문
✅ 워크북 체크리스트
✅ 컨벤션 체크리스트