【问题标题】:I need to get the current URL, when the pagination changes in React当 React 中的分页发生变化时,我需要获取当前的 URL
【发布时间】: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


【解决方案1】:
window.location.href this works but at different conditions not at this, you must use some inbuilt library like `react-router-dom` of navigation or Link so that it takes you to change your route apply this and check once again if you have any problems then revert me back.?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 2016-03-10
    • 2011-10-24
    • 1970-01-01
    相关资源
    最近更新 更多