【发布时间】:2022-11-28 18:00:03
【问题描述】:
import { PageLoad} from './PageLoad'
useEffect(() => {
PageLoad();
}, [currentPage]);
// function in different js
const PageLoad= () => {
const URL = window.location.href;
console.log(URL);
}
当我更改分页时,我需要页面的当前 URL,当组件加载时,我正在正确获取 URL。例如:my-lists.html?page=1 当我将分页更改为第二页时,URL 是 my-lists.html?page=2 但在控制台中我仍然得到 my-lists.html?page=1 甚至认为我当前的 url 是 my-lists.html?page =2。
【问题讨论】:
-
你如何设置
currentPage? -
你在使用任何为你处理路由的东西吗?喜欢
next/router或react-router-dom?单页应用程序 (SPA) 不像普通页面那样处理路由。甚至你当前的路线对于.html扩展名也有点奇怪。 -
@GabrielePetrioli,嗨,currentPage 是状态,我需要在该状态更改时获取 url。
-
@AbsoluteZero,是的,我正在使用 react-router-dom。所以我需要的只是在状态 currentPage 更改时获取当前 url 并将其更新为不同的状态。
-
那你为什么用
window.location.href而不是this?
标签: javascript reactjs react-native react-hooks react-redux