I'm trying to access the URL subdomain. Traditionally in JavaScript I'd do
var full = window.location.host;
var parts = full.split('.');
var subdomain = parts[0];
// ...
However I'm calling this from a React JS component, and I get the following error -
Encountered error "TypeError: undefined is not an object (evaluating 'window.location.host')"
It seems as though window.location.host
doesn't work from React? Does it not have access to the window?
I get there might be philosophical reasons to not do this, but I'd still be curious how to implement it if it's possible from React.
Thanks!
This is isomorphic so if it is being processed server side so you cannot access the window.location.href
For server side, you will need to look at the underlying request object to get the hostname:
http://expressjs.com/en/api.html#req.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