今天我们将了解如何从头开始创建分页并使其可访问和可重用。希望对您有帮助,欢迎在文末留言评论!
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中文网其他相关文章!