I can parse are HTTP response of one of the intial calls and save that in the state but there must be another way to get the URL?
I am using the npm 'rest' library and want to specify a different port but I do not know the path on which machine the server will be installed so I need the server's url.
Solution
window.location.hostname
Use the window object to get the current URL in React, e.g. window. location. href returns a string containing the whole URL. If you need to access the path, use window.
Using location props location. pathname can get you the current URL pathname inside a React component.
To display an image from a URL, use the img tag and set its src prop to the complete URL of the image. Optionally set the alt prop to a short description of the image.
location
is a property on the window
object with a bunch of information, you can get the url with window.location.href
You can get the hostname with window.location.hostname
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With