I need to get parameters in a hooks component, what's the cleanest way without using react-routes? Is windows.location.search enough? How to use it in a clean way with hooks?
-
Hey Marianne. You don't need to do anything special in the "hook" context - any Javascript code can run in it. The answers in this question will work in any hook you create - stackoverflow.com/questions/39823681/…Moshe Jonathan Gordon Radian– Moshe Jonathan Gordon Radian2021-02-05 22:36:26 +00:00Commented Feb 5, 2021 at 22:36
-
Does this answer your question? Read the current full URL with React?Moshe Jonathan Gordon Radian– Moshe Jonathan Gordon Radian2021-02-05 22:37:32 +00:00Commented Feb 5, 2021 at 22:37
-
Thank you guys, I was wondering if it's considered bad practice writing window element in react directly, or it's just fine. Thanks!Marianne Luipon– Marianne Luipon2021-02-06 08:29:40 +00:00Commented Feb 6, 2021 at 8:29
Add a comment
|
1 Answer
It has less to do with react if you don't want to use React Router:
let paramsString = window.location.search
let searchParams = new URLSearchParams(paramsString)
searchParams.has("somequery"); // true if it is present
To parse the search params, you can use URLSearchParams.
1 Comment
Marianne Luipon
I was able to read on developer.mozilla.org as well alone, my question is about how to use a clean way with hooks.