{"id":2043,"date":"2025-05-26T23:28:28","date_gmt":"2025-05-26T15:28:28","guid":{"rendered":"https:\/\/www.tonghin.com.my\/blog\/?p=2043"},"modified":"2025-05-26T23:28:30","modified_gmt":"2025-05-26T15:28:30","slug":"preserving-pagination-state-with-url-syncing","status":"publish","type":"post","link":"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/preserving-pagination-state-with-url-syncing\/","title":{"rendered":"Preserving Pagination State with URL Syncing"},"content":{"rendered":"\n<p>Monday, May 5, 2025 \u2013 Last week, I worked on a task where users view a paginated list of data, and when they click into a detail page and return, they should land back on the same page they left. However, the page would always reset to page 1, which was frustrating and not user-friendly. Initially, I was confused because I couldn&#8217;t find any logic in the code that explicitly reset the page to 1. Since the project had many layers of logic, Mr. Peter advised me to comment out everything and test step by step to identify the root cause. Following his advice, I eventually discovered that a custom pagination hook was resetting the page to 1 each time. After removing the part of the hook responsible for this, the issue was resolved, the page no longer jumped back to the first one when returning from a detailed view.<\/p>\n\n\n\n<p>Once that was fixed, the next goal was to make pagination even more seamless by syncing the page state with the URL. I created a custom hook that reads the page number from the URL and updates it dynamically as users navigate. This means users can now move between pages, use the browser&#8217;s back\/forward buttons, and even share links with the exact page state preserved. The hook keeps the UI state and URL in sync, making navigation more intuitive and consistent. This improvement made the pagination feel much more natural and user-friendly, especially when dealing with long lists of data or when users need to return to the same context after viewing details.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Monday, May 5, 2025 \u2013 Last week, I worked on a task where users view a paginated list of data, and when they click into a detail page and return, they should land back on the same page they left. However, the page would always reset to page 1, which was frustrating and not user-friendly. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/preserving-pagination-state-with-url-syncing\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Preserving Pagination State with URL Syncing&#8221;<\/span><\/a><\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Preserving Pagination State with URL Syncing | Tong Hin&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/preserving-pagination-state-with-url-syncing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Preserving Pagination State with URL Syncing | Tong Hin&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"Monday, May 5, 2025 \u2013 Last week, I worked on a task where users view a paginated list of data, and when they click into a detail page and return, they should land back on the same page they left. However, the page would always reset to page 1, which was frustrating and not user-friendly. &hellip; Continue reading &quot;Preserving Pagination State with URL Syncing&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/preserving-pagination-state-with-url-syncing\/\" \/>\n<meta property=\"og:site_name\" content=\"Tong Hin&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-26T15:28:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-26T15:28:30+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"2 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#organization\",\"name\":\"TONG HIN MACHINERY\",\"url\":\"https:\/\/www.tonghin.com.my\/blog\/\",\"sameAs\":[\"https:\/\/www.youtube.com\/channel\/UC60EoxJMNbu8mV0LuEGfrBg\"],\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#logo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/www.tonghin.com.my\/blog\/wp-content\/uploads\/2020\/10\/web_logo_180_180.jpg\",\"width\":180,\"height\":180,\"caption\":\"TONG HIN MACHINERY\"},\"image\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#logo\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#website\",\"url\":\"https:\/\/www.tonghin.com.my\/blog\/\",\"name\":\"Tong Hin&#039;s Blog\",\"description\":\"Sharing About Technology\",\"publisher\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.tonghin.com.my\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/preserving-pagination-state-with-url-syncing\/#webpage\",\"url\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/preserving-pagination-state-with-url-syncing\/\",\"name\":\"Preserving Pagination State with URL Syncing | Tong Hin&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#website\"},\"datePublished\":\"2025-05-26T15:28:28+00:00\",\"dateModified\":\"2025-05-26T15:28:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/preserving-pagination-state-with-url-syncing\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/preserving-pagination-state-with-url-syncing\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/preserving-pagination-state-with-url-syncing\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/\",\"url\":\"https:\/\/www.tonghin.com.my\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/preserving-pagination-state-with-url-syncing\/\",\"url\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/preserving-pagination-state-with-url-syncing\/\",\"name\":\"Preserving Pagination State with URL Syncing\"}}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/preserving-pagination-state-with-url-syncing\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/preserving-pagination-state-with-url-syncing\/#webpage\"},\"author\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#\/schema\/person\/81e2366362d8d14b15519395755cee6f\"},\"headline\":\"Preserving Pagination State with URL Syncing\",\"datePublished\":\"2025-05-26T15:28:28+00:00\",\"dateModified\":\"2025-05-26T15:28:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/preserving-pagination-state-with-url-syncing\/#webpage\"},\"publisher\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#organization\"},\"articleSection\":\"Experiential\",\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#\/schema\/person\/81e2366362d8d14b15519395755cee6f\",\"name\":\"nurlailiyuzi\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5c0d4cc6f7fc881bd56d95bdbb0c4c14?s=96&d=mm&r=g\",\"caption\":\"nurlailiyuzi\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/posts\/2043"}],"collection":[{"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/comments?post=2043"}],"version-history":[{"count":2,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/posts\/2043\/revisions"}],"predecessor-version":[{"id":2049,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/posts\/2043\/revisions\/2049"}],"wp:attachment":[{"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/media?parent=2043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/categories?post=2043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/tags?post=2043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}