Skip to content

Commit c03ebb0

Browse files
committed
fix: 페이지를 나가도 글이 캐싱되어 있는 문제 수정
1 parent 5418e92 commit c03ebb0

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/hooks/usePrompt.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useEffect } from 'react';
22
import { useBlocker } from 'react-router-dom';
33

44
// should be isDirty
5-
export const usePrompt = (when: boolean) => {
5+
export const usePrompt = (when: boolean, onLeave?: () => void) => {
66
const blocker = useBlocker(({ currentLocation, nextLocation }) => {
77
return when && currentLocation.pathname !== nextLocation.pathname;
88
});
@@ -14,15 +14,15 @@ export const usePrompt = (when: boolean) => {
1414

1515
useEffect(() => {
1616
if (blocker.state !== 'blocked') return;
17-
console.log(when);
1817
if (!when) return;
1918

2019
if (window.confirm('정말로 이동하시겠습니까?')) {
20+
onLeave?.();
2121
blocker.proceed();
2222
} else {
2323
blocker.reset();
2424
}
25-
}, [blocker.state]);
25+
}, [blocker.state, when, onLeave]);
2626

2727
useEffect(() => {
2828
if (when) {

src/pages/Posting/Posting.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import ArticleFeedbackPanel from '@/components/features/Feedback/ArticleFeedback
1515
import PostEditor from '@/components/features/Post/PostEditor';
1616
import { CommonLayout } from '@/components/layout/CommonLayout';
1717

18-
import { useEffect, useState } from 'react';
18+
import { useCallback, useEffect, useState } from 'react';
1919
import { RiErrorWarningFill } from 'react-icons/ri';
2020
import {
2121
Navigate,
@@ -107,7 +107,14 @@ const Posting = () => {
107107
const buttonFontSize = isMobile ? '12px' : '16px';
108108
const [progress, setProgress] = useState(0);
109109

110-
usePrompt(!disablePrompt);
110+
const clearPostingCache = useCallback(() => {
111+
sessionStorage.removeItem(`posting-articleId-${id}`);
112+
sessionStorage.removeItem(`posting-content-${id}`);
113+
sessionStorage.removeItem(`posting-title-${id}`);
114+
setPostImages([]);
115+
}, [id, setPostImages]);
116+
117+
usePrompt(!disablePrompt, clearPostingCache);
111118

112119
useEffect(() => {
113120
document.documentElement.scrollTo({ top: 0, left: 0, behavior: 'instant' });
@@ -250,9 +257,7 @@ const Posting = () => {
250257
const savedId = await handleSaveArticle();
251258

252259
if (savedId) {
253-
sessionStorage.removeItem(`posting-articleId-${id}`);
254-
sessionStorage.removeItem(`posting-content-${id}`);
255-
sessionStorage.removeItem(`posting-title-${id}`);
260+
clearPostingCache();
256261
setDashboardView('article');
257262
setSelectedPostId(savedId);
258263
setDisablePrompt(true);

0 commit comments

Comments
 (0)