reactjs 当React中的分页发生更改时,我需要获取当前URL

jv4diomz  于 2022-11-29  发布在  React
关注(0)|答案(1)|浏览(139)
import { PageLoad} from './PageLoad'
import { useLocation } from "react-router-dom";
  
const Pagination = (props) => {
    const location = useLocation();
    const { currentPage, setPage, totalPages } = props.pageControl;
useEffect(() => {
        PageLoad(location.pathname);
    }, [currentPage]);
}
// function in different js

const PageLoad= (URL) => {
console.log(URL);
}

// parent component
    const [paginationValues, paginationApi] = usePagination();
const { currentPage, totalPages } = paginationValues;
const { setCurrentPage, setTotalPages } = paginationApi;
const pageControl = {
        currentPage,
        setPage: setCurrentPage,
        totalPages,
        tileBuffer: 5
    };

当我更改分页时,我需要页面的当前URL,当组件加载时,我可以正确获取URL。例如:my-lists.html?page=1当我将分页更改为第二页时,URL是my-lists.html?page=2,但在控制台中,即使我的当前URL是my-lists. html?page=2,我仍然可以获取my-lists. html?page=1。

qlzsbp2j

qlzsbp2j1#

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.👍

相关问题