I am working on ReactJs and the server direct to a domain name with id on it. So I want to ask how can I get current domain name with ReactJS?
3 Answers
This isn't React specific.
Just look at window.location:
alert(window.location.hostname)
(Yes, the above snippet will say stacksnippets.net due to security reasons.)
4 Comments
Tong
Thanks, but I got this error:
ReferenceError: window is not defined. It is jsdom jsdom-global library right?AKX
window is innately available in browsers, you don't need a library. Are you running this in JSDom?Tong
I found the answer. I use
context.req.url of getInitialProps ReactJS, so I dont have to use window.AKX
There is no "context.req.url" in raw ReactJS, so you left essential detail out of your original question. Glad you found the answer though.
Use window.location inside componentDidMount React Lifecycle. If you want to use inside DOM use state to store value.
1 Comment
Tong
I found the answer. I use
context.req.url of getInitialProps ReactJS, so I dont have to use window.