Skip to content

Commit

Permalink
refactor: 로컬스토리지에서 게임 기록을 가져오는 로직을 별도의 함수로 분리
Browse files Browse the repository at this point in the history
maylh committed Nov 9, 2024
1 parent 1ba5a7b commit 8c313cf
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions week3/assignment/src/components/Ranking.jsx
Original file line number Diff line number Diff line change
@@ -4,15 +4,21 @@ import styled from '@emotion/styled';
const Ranking = () => {
const [records, setRecords] = useState([]);

useEffect(() => {
const fetchGameRecords = () => {
const storedRecords = JSON.parse(localStorage.getItem('gameRecords')) || [];

const sortedRecords = storedRecords.sort((a, b) => {
if (b.level === a.level) {
return a.playTime - b.playTime;
}
return b.level - a.level;
});

return sortedRecords;
};

useEffect(() => {
const sortedRecords = fetchGameRecords();
setRecords(sortedRecords);
}, []);

0 comments on commit 8c313cf

Please sign in to comment.