1

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.

enter image description here

Second console.log(window.location.pathname) which returns previous visited page value.

enter image description here

TMA
  • 1,419
  • 2
  • 22
  • 49

2 Answers2

0

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

get location

Roopak Puthenveettil
  • 1,387
  • 2
  • 13
  • 27
  • It returns me a TypeError of `props` undefined while using a `this.props.location` in this redux function. – TMA Aug 30 '19 at 09:17
0

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.

Nappy
  • 3,016
  • 27
  • 39