0

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

d-dripz
  • 61
  • 6

2 Answers2

1

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

web-sudo
  • 521
  • 12
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?

FLiotta
  • 142
  • 6