supasate / connected-react-router

A Redux binding for React Router v4
MIT License
4.73k stars 593 forks source link

"how-to-get-the-current-browser-location-url " in my reducer #200

Open 951565664 opened 5 years ago

951565664 commented 5 years ago

I konw how-to-get-the-current-browser-location-url in Component or Container, but I want get the current browser location url in my Reducer. I don't want to pass it with param What's the best way to get the current browser location url in my reducer?

951565664 commented 5 years ago

window.location.herf can get the current browser url, but it is not use "connected-react-router"

antgonzales commented 5 years ago

@951565664 it's not generally a good practice to have different reducers reading from the state of other reducers. This is kind of the role of side effects middleware like Redux Thunks or Redux Sagas. If you want an action to occur as a result of a specific location, you should dispatch an action to your reducer using a side effect library.

951565664 commented 5 years ago

@951565664 it's not generally a good practice to have different reducers reading from the state of other reducers. This is kind of the role of side effects middleware like Redux Thunks or Redux Sagas. If you want an action to occur as a result of a specific location, you should dispatch an action to your reducer using a side effect library.

@antgonzales why "it's not generally a good practice to have different reducers reading from the state of other reducers"?I just want all the logic to be processed in the xxRedux.js.I think that can improve the maintainability of the code. I have used Redux Thunks,

robchristian commented 5 years ago

Just read from state.router.location.pathname in mapStateToProps. .router may have a different name for you, depending on your setup. Use redux devtools to see your state tree.

Screen Shot 2019-04-25 at 4 50 11 PM

A limitation of reducers is that they can only read from the slice of state you assigned. You can use a library like redux-logic, or thunks, to read from the entire tree.