Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Saving parts of URL information for reuse in React

How can one take the information of a URL?

I want to use the appended information on this URL http://localhost:3000/transaction?transactionId=72U8ALPE in a fetch API.

I am trying to take the value 72U8ALPE and save it to state or as a variable. Then use if on the URL I will use on a fetch request. For example

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

https://google.com/${the-url-last-part-saved-as-variable-or-in-state}

>Solution :

You can get the query parameters from the URL object.
This could look something like this:

let currentUrl = new URL(window.location);
let transactionId = currentUrl.searchParams.get('transactionId');

You can then use the variable to append to your HTTP request.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading