Recent Posts
Recent Comments
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- ๋ฐ๋ธ์ฝ์ค3๊ธฐ
- ํ๋ก ํธ์๋
- ๋ธ๋ก๊ทธ
- history api
- position
- fetch API
- CSS
- ์๊ณ ๋ฆฌ์ฆ
- ํ๋ก๊ทธ๋๋จธ์ค
- Props
- ๋ฐ๋ธ์ฝ์ค
- ์๋ฐ์คํฌ๋ฆฝํธ
- Flex
- ์ฝ๋ฉํ ์คํธ
- useEffect
- Gatsby
- useRef
- REACT
- float
Archives
- Today
- Total
Daehyunii's Dev-blog
์ฌ์ฉ์ฌ๋ก - ํ์ด์ง๋ค์ด์ ๋ง๋ค๊ธฐ ๋ณธ๋ฌธ
๐ Language & CS knowledge/React
์ฌ์ฉ์ฌ๋ก - ํ์ด์ง๋ค์ด์ ๋ง๋ค๊ธฐ
Daehyunii 2022. 12. 18. 23:58App.js
import { useState } from "react";
import Board from "./components/Board";
import Pagination from "./components/Pagination";
function App() {
const [page, setPage] = useState(0);
const articles = new Array(100).fill().map((_, i) => {
return {
id: i,
title: `${i}๋ฒ ๊ฒ์๋ฌผ`,
};
});
const limit = 6;
const offset = page * limit;
return (
<div>
<Pagination
defaultPage={0}
limit={limit}
total={articles.length}
onChange={setPage}
/>
<Board articles={articles.slice(offset, offset + limit)} />
</div>
);
}
export default App;
Pagination.js
import { useState } from "react";
const Pagination = ({ defaultPage, limit, total, onChange }) => {
const [page, setPage] = useState(defaultPage);
const totalPage = Math.ceil(total / limit);
const handleChangePage = (newPage) => {
onChange(newPage);
setPage(newPage);
};
return (
<div>
<button
onClick={() => {
page !== 0 && handleChangePage(page - 1);
}}
>
์ด์
</button>
{Array.from(new Array(totalPage), (_, i) => i)
.filter((i) => {
if (page < 3) {
return i < 5;
} else if (page > totalPage - 3) {
return i >= totalPage - 5;
}
return i >= page - 2 && i <= page + 2;
})
.map((i) => (
<button
key={i}
onClick={() => handleChangePage(i)}
style={{ backgroundColor: page === i ? "red" : undefined }}
>
{i + 1}
</button>
))}
<button
onClick={() => {
page + 1 !== totalPage && handleChangePage(page + 1);
}}
>
๋ค์
</button>
</div>
);
};
export default Pagination;
Board.js
import PropTypes from "prop-types";
const Board = ({ articles }) => {
return (
<ul>
{articles.map((article) => (
<li key={article.id}>
{article.id} | {article.title}
</li>
))}
</ul>
);
};
Board.propTypes = {
articles: PropTypes.array,
};
export default Board;
'๐ Language & CS knowledge > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JSX๋? (0) | 2022.12.20 |
---|---|
์ปดํฌ๋ํธ ์คํ์ผ๋ง, useMemo, useCallback, Custom Hook (0) | 2022.12.19 |
React, useEffect, useRef, props (0) | 2022.12.18 |
classํ ์ปดํฌ๋ํธ - pureComponent (0) | 2022.12.16 |
classํ ์ปดํฌ๋ํธ - shouldComponentUpdate( ) (0) | 2022.12.15 |