React-redux gets the base URL of the site / window.location

Hope this is a very simple question:

I want to create a string containing the full URL of a page on my site, for example:

https://example.com/documents/1

Ideally, I would like to create this in a container with a connect() reduction reaction, in mapStateToProps() . Where is the page a grandson of the responsive Route router using browserHistory (so maybe I can get it from agent-router or browserHistory?) If necessary, I will do this in the fully functional React class.

But I can’t let my life know how to do it. window (as in window.location ) is always undefined in mapStateToProps() (there is no big surprise here) and, in my opinion, the render() component.

So far, I only found ways to access the current route, for example. "/ documents / 1", which is only a relative path and does not contain the URL of the site database ( https://example.com/ )

+5
source share
1 answer

So, firstly, remember that your code works on the client and server, so any access to the window should be wrapped in a check.

  if (typeof window !== 'undefined') { var path = location.protocol + '//' + location.host + '/someting'; // (or whatever) } else { // work out what you want to do server-side... } 

If the URL you created is displayed in the DOM, and on the client you populate the repository with this URL before the first rendering, you will receive a checksum error. If you really want to avoid the error, you can wait until componentDidMount() in the root component to get / set the URL.

+9
source

All Articles