-1

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?

3
  • 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/… Commented Feb 5, 2021 at 22:36
  • Does this answer your question? Read the current full URL with React? Commented 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! Commented Feb 6, 2021 at 8:29

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was able to read on developer.mozilla.org as well alone, my question is about how to use a clean way with hooks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.