In my react project i need to make a condition on redux action according to the page location.
So i have used window.location.href
to get value of the current page but it returns the last visited page value.
export const getBlog = (type, offset) => {
console.log(window);
console.log(window.location.pathname);
if( window.location.pathname !== '/planner' ){
return (dispatch) => {
dispatch({ type: 'BLOG_LOADING_START'})
axios.get(siteurl + TPW.CATEGORY_API + type, {
headers: { 'Content-Type': 'application/json' } })
.then(response => {
let ary = response.data.slice(0, offset)
dispatch({
type: 'SET_BLOG_DATA',
list: response.data,
blogData: ary,
total: response.data.length,
offSetCnt: 3
})
dispatch({ type: 'BLOG_LOADING_DONE'})
});
}
}
}
First console.log(window)
screenshot; which returns an actual visited page location result.
Second console.log(window.location.pathname)
which returns previous visited page value.
window.location.pathname
always gives you the page you landed on. Since we are using react-router
to get the current route you need to use this.props.location
.
You can refer this answer for more clarity
I believe the console does not make a deep copy of window when you log it. It loads the value when you expand the location node.
That explains why those two results differ.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.