今天我們將了解如何從頭開始建立分頁並使其可存取和可重複使用。希望對您有幫助,歡迎在文末留言評論!
Github:https://github.com/micaavigliano/accessible-pagination
項目:https://accessible-pagination.vercel.app/
const useFetch = <T,>(url: string, currentPage: number = 0, pageSize: number = 20) => { const [data, setData] = useState<T | null>(null); const [loading, setLoading] = useState<boolean>(true); const [error, setError] = useState<boolean>(false); useEffect(() => { const fetchData = async() => { setLoading(true); setError(false); try { const response = await fetch(url); if (!response.ok) { throw new Error('network response failed') } const result: T = await response.json() as T; setData(result) } catch (error) { setError(true) } finally { setLoading(false); } }; fetchData() }, [url, currentPage, pageSize]); return { data, loading, error, } };
為了使頁面可訪問,我們必須考慮以下幾點:
aria-pointset 用於計算該項目在頁面上所有項目中的位置。螢幕閱讀器將如下宣布:
為了實現這一點,我們將對其進行如下編碼:
const useFetch = <T,>(url: string, currentPage: number = 0, pageSize: number = 20) => { const [data, setData] = useState<T | null>(null); const [loading, setLoading] = useState<boolean>(true); const [error, setError] = useState<boolean>(false); useEffect(() => { const fetchData = async() => { setLoading(true); setError(false); try { const response = await fetch(url); if (!response.ok) { throw new Error('network response failed') } const result: T = await response.json() as T; setData(result) } catch (error) { setError(true) } finally { setLoading(false); } }; fetchData() }, [url, currentPage, pageSize]); return { data, loading, error, } };
當頁面停止載入時,我們將使用 currentPage 和我們正在載入的新陣列的長度來設定一條新訊息。
現在是的!讓我們看看檔案 pagination.tsx
中的程式碼結構如何此組件需要五個道具
const [statusMessage, setStatusMessage] = useState<string>(""); useEffect(() => { window.scrollTo({ top: 0, behavior: 'smooth' }); if (!loading) { setStatusMessage(`Page ${currentPage} loaded. Displaying ${data?.near_earth_objects.length || 0} items.`); } }, [currentPage, loading]);
interface PaginationProps { currentPage: number; totalPages: number; nextPage: () => void; prevPage: () => void; goToPage: (page: number) => void; }
const handlePageChange = (newPage: number) => { setCurrentPage(newPage); }; const nextPage = () => { if (currentPage < totalPages) { handlePageChange(currentPage + 1); } };
const prevPage = () => { if (currentPage > 1) { handlePageChange(currentPage - 1); } };
為了使我們的分頁變得生動,我們還需要一步,創建我們將在列表中迭代的數組!為此,我們必須遵循以下步驟:
我們將透過該數組來獲取頁面中的項目列表,如下所示:
const useFetch = <T,>(url: string, currentPage: number = 0, pageSize: number = 20) => { const [data, setData] = useState<T | null>(null); const [loading, setLoading] = useState<boolean>(true); const [error, setError] = useState<boolean>(false); useEffect(() => { const fetchData = async() => { setLoading(true); setError(false); try { const response = await fetch(url); if (!response.ok) { throw new Error('network response failed') } const result: T = await response.json() as T; setData(result) } catch (error) { setError(true) } finally { setLoading(false); } }; fetchData() }, [url, currentPage, pageSize]); return { data, loading, error, } };
以下是如何製作可重複使用且易於存取的分頁!就我個人而言,我學習瞭如何從頭開始創建頁面,因為我必須在即時編碼中實現它,我希望我的經驗對您的職業生涯有所幫助,並且您可以實現甚至改進它!
您好,
雲母
以上是可存取的元件:分頁的詳細內容。更多資訊請關注PHP中文網其他相關文章!