Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the URL hostname from React JS

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!

like image 795
user2490003 Avatar asked May 19 '16 18:05

user2490003


1 Answers

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

like image 133
Steve O Avatar answered Oct 18 '22 01:10

Steve O