update pagination if page param changes

This commit is contained in:
zeppi 2021-11-05 15:09:06 -04:00 committed by jessopb
parent 32988bb7ca
commit 41edd8317c

View file

@ -20,7 +20,8 @@ function Paginate(props: Props) {
const { search } = location; const { search } = location;
const [textValue, setTextValue] = React.useState(''); const [textValue, setTextValue] = React.useState('');
const urlParams = new URLSearchParams(search); const urlParams = new URLSearchParams(search);
const initialPage = disableHistory ? 1 : Number(urlParams.get(PAGINATE_PARAM)) || 1; const urlParamPage = Number(urlParams.get(PAGINATE_PARAM));
const initialPage = disableHistory ? 1 : urlParamPage || 1;
const [currentPage, setCurrentPage] = React.useState(initialPage); const [currentPage, setCurrentPage] = React.useState(initialPage);
const isMobile = useIsMobile(); const isMobile = useIsMobile();
@ -47,6 +48,12 @@ function Paginate(props: Props) {
} }
} }
React.useEffect(() => {
if (urlParamPage) {
setCurrentPage(urlParamPage);
}
}, [urlParamPage, setCurrentPage]);
return ( return (
// Hide the paginate controls if we are loading or there is only one page // Hide the paginate controls if we are loading or there is only one page
// It should still be rendered to trigger the onPageChange callback // It should still be rendered to trigger the onPageChange callback