Skip to content

Instantly share code, notes, and snippets.

@NeoYo
Created September 17, 2021 13:26
Show Gist options
  • Save NeoYo/5e17d88eca4f5c752a1c594d27a0a371 to your computer and use it in GitHub Desktop.
Save NeoYo/5e17d88eca4f5c752a1c594d27a0a371 to your computer and use it in GitHub Desktop.
How to Use React useEffect - PageSelector
function PageSelector() {
const [page, setPage] = useState(1);
useEffect(() => {
// Get page params from URLSearchParams
const urlParams = new URLSearchParams(window.location.search);
const params = Object.fromEntries(urlSearchParams.entries());
// Update page state if exist
if (params?.page) {
setPage(params.page);
}
}, []);
// ...
return (
<p>{page}</p>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment