[英]ReactJS: onClick `window.location.href` return previous page value
在我的React項目中,我需要根據頁面位置來確定redux操作的條件。
所以我已經使用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'})
});
}
}
}
首先console.log(window)
屏幕截圖; 返回實際訪問頁面的位置結果。
第二個console.log(window.location.pathname)
返回前一次訪問的頁面值。
window.location.pathname
始終為您提供登陸的頁面。 由於我們使用react-router
來獲取當前路由,因此您需要使用this.props.location
。
您可以參考此答案以更清楚
我相信控制台在您登錄時不會對窗口進行深層復制。 展開位置節點時,它將加載值。
這就解釋了為什么這兩個結果不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.