简体   繁体   English

ReactJS:onClick`window.location.href`返回上一页值

[英]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 您可以参考此答案以更清楚

get location 获取位置

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.

相关问题 如何根据返回值更改window.location.href? - How to change window.location.href according to return value? 等待window.location.href吗? - Await window.location.href? 如何在ReactJS中重定向到新页面,例如www.google.com,例如单击一个按钮window.location.href - How to Redirect to a new page like www.google.com in ReactJS like click a button window.location.href ReactJS + React Cookie + React Router:为什么window.location.href无法正常工作? - ReactJS + React Cookie + React Router: Why doesn't window.location.href work properly? 如何在 ComponentWillMount 中使用 window.location.href? - How to use window.location.href in ComponentWillMount? Gatsby\React - window.location.href 不工作 - Gatsby\React - window.location.href not working 如何向 window.location.href 添加组件? - How to add a component to window.location.href? javascript 中的 window.location.href 只是重新加载页面,而不是导航到另一个网站 - window.location.href in javascript just reloading page, not navigating to another website 为什么在_app.js中添加window.location.href 接下来制作always reload page - Why Adding window.location.href in _app.js Next making always reload page 使用window.location.href重定向React中的页面时,Axios无法正常工作 - Axios not working properly when using window.location.href to redirect page in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM