[251120] refactor: hooks - usePrevious.js, usePrevious.test.js, usePreviou...

🕐 커밋 시간: 2025. 11. 20. 06:13:41

📊 변경 통계:
  • 총 파일: 3개
  • 추가: +71줄
  • 삭제: -74줄

📝 수정된 파일:
  ~ com.twin.app.shoptime/src/hooks/usePrevious.js
  ~ com.twin.app.shoptime/src/hooks/usePrevious.test.js
  ~ com.twin.app.shoptime/src/hooks/usePreviousExample.jsx

🔧 주요 변경 내용:
  • 핵심 비즈니스 로직 개선
  • 테스트 커버리지 및 안정성 향상
  • 소규모 기능 개선
  • 코드 정리 및 최적화

Performance: 코드 최적화로 성능 개선 기대
This commit is contained in:
2025-11-20 06:13:41 +09:00
parent 78bf217d75
commit 18175a03de
3 changed files with 71 additions and 74 deletions

View File

@@ -8,13 +8,13 @@ import usePrevious from './usePrevious';
// 예시 1: 단일 값 추적 - 카운터
export function CounterExample() {
const [count, setCount] = useState(0);
const prevCount = usePrevious(count);
const prevCountRef = usePrevious(count);
return (
<div style={{ padding: '20px', border: '1px solid #ccc', marginBottom: '20px' }}>
<h3>예시 1: 단일 추적 (카운터)</h3>
<p>현재값: {count}</p>
<p>이전값: {prevCount !== undefined ? prevCount : '초기값'}</p>
<p>이전값: {prevCountRef.current !== undefined ? prevCountRef.current : '초기값'}</p>
<button onClick={() => setCount(count + 1)}>증가</button>
<button onClick={() => setCount(count - 1)}>감소</button>
</div>
@@ -27,7 +27,8 @@ export function UserFormExample() {
const [age, setAge] = useState('');
const [email, setEmail] = useState('');
const prev = usePrevious({ name, age, email });
const prevRef = usePrevious({ name, age, email });
const prev = prevRef.current;
const hasNameChanged = prev?.name !== name;
const hasAgeChanged = prev?.age !== age;
@@ -92,7 +93,8 @@ export function ArrayValuesExample() {
const [b, setB] = useState(0);
const [c, setC] = useState(0);
const [prevA, prevB, prevC] = usePrevious([a, b, c]) || [];
const prevRef = usePrevious([a, b, c]);
const [prevA, prevB, prevC] = prevRef.current || [];
return (
<div style={{ padding: '20px', border: '1px solid #ccc', marginBottom: '20px' }}>
@@ -150,7 +152,8 @@ export function ChangeDetectionExample() {
views: 1000,
});
const prevData = usePrevious(data);
const prevDataRef = usePrevious(data);
const prevData = prevDataRef.current;
const changes = {
titleChanged: prevData?.title !== data.title,