1

I am trying to grab XSOQSVPbATbRYa94ZuXs from the pid parameter using URLSearchParams.

Here is my URL: http://localhost:3000/edit-product?pid=Z4HLrHGZ1ikKIwlEVkM6

Here is my code:

useEffect(() => {
    if (user) {
      const params = new URLSearchParams(location.search);
      const pid = params.get("pid");

    } else {
      setProducts("");
    }
  }, [user]);

Any idea why I'm receiving the following error: TypeError: Cannot read property 'search' of undefined

2 Answers 2

1

location.search won't work. You need to change it by window.location.search

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

Comments

0

Where does location.search comes from? unless you destructured it from somewhere, it should be props.location.search if you're using react-router.

Is this question related?

Comments

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.