Created
September 17, 2021 13:26
-
-
Save NeoYo/5e17d88eca4f5c752a1c594d27a0a371 to your computer and use it in GitHub Desktop.
How to Use React useEffect - PageSelector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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