[英]ReactJS: onClick `window.location.href` return previous page value
In my react project i need to make a condition on redux action according to the page location. 在我的React项目中,我需要根据页面位置来确定redux操作的条件。
So i have used window.location.href
to get value of the current page but it returns the last visited page value. 所以我已经使用
window.location.href
来获取当前页面的值,但是它返回了上次访问的页面值。
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; 首先
console.log(window)
屏幕截图; which returns an actual visited page location result. 返回实际访问页面的位置结果。
Second console.log(window.location.pathname)
which returns previous visited page value. 第二个
console.log(window.location.pathname)
返回前一次访问的页面值。
window.location.pathname
always gives you the page you landed on. window.location.pathname
始终为您提供登陆的页面。 Since we are using react-router
to get the current route you need to use this.props.location
. 由于我们使用
react-router
来获取当前路由,因此您需要使用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. 这就解释了为什么这两个结果不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.