0

I am new to react/redux and I was wondering if there is a method to pull variables out of an url example www.url.com/(123) I wish to take the 123 outta it and pass it through other functions but am at lost at this point.

Ken Tay
  • 57
  • 1
  • 1
  • 6
  • Take a look to this answer: https://stackoverflow.com/questions/35352638/how-to-get-parameter-value-from-query-string – Andoni Aug 16 '18 at 08:37

2 Answers2

2

This isn't a React question, just a general JavaScript one.

One way to do it is:

const url = 'http://www.url.com/123';
const path = new URL(url).pathname;
console.log(path);

Another way is to split the URL into an array of segments.

let path = window.location.pathname.split( '/' );
Paul Redmond
  • 3,276
  • 4
  • 32
  • 52
  • what about 2 different components, the first is to link to the next page and the second component is to catch the variable at the end of the link will this work too? cause i cant figure out how to put this into my code. – Ken Tay Aug 16 '18 at 08:57
  • You can use `split` to grab the different URL segments. Updated above. – Paul Redmond Aug 16 '18 at 09:02
0

If I understood your question, you should be using location.pathname to get that ;)

evandrolg
  • 65
  • 2
  • 10