React router dom v6 - 导航(-1)刷新页面时需要按2次

问题描述 投票:0回答:1

所以我将

react
react-router-dom
v6 一起使用。我发现当我导航到第一个页面时,在该页面中,如果我刷新页面,则需要按下按钮调用
navigate(-1)
2 次,并且返回上一页所需的时间会随着刷新次数的增加而增加那一页。

所以有什么办法可以解决这个问题吗?即使我刷新页面多少次

navigate(-1)
总是会回到最后一页?

import { CButton } from '@coreui/react-pro';
import { useNavigate } from 'react-router-dom';

const ProductionStatisticsManagement = () => {
  const navigate = useNavigate();

  return (
    <>
      ProductionStatisticsManagement
      <CButton onClick={() => navigate('/production-statistics/update')}>Redirect</CButton>
    </>
  );
};

export default ProductionStatisticsManagement;

...

import { CButton } from '@coreui/react-pro';
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';

const ProductionStatisticsUpdate = () => {
  const navigate = useNavigate();

  const [count, setCount] = useState<number>(0);

  const handleCountNavigate = () => {
    navigate(-1);
    setCount(count + 1);
  };
  return (
    <>
      ProductionStatisticsUpdate Button press:{count}
      <CButton onClick={handleCountNavigate}>Redirect</CButton>
    </>
  );
};

export default ProductionStatisticsUpdate;
javascript reactjs react-router react-router-dom
1个回答
1
投票

我也有同样的问题。我研究了调用堆栈,看到调用

navigate(other_page)
两次以转到另一个页面。

useEffect(() => {
   if (isSuccess) {
      navigate('/other_page')
   }
}, [...])

other_page
上有可调用
navigate(-1)
的按钮,我需要按两次才能返回导航。 我更改了 useEffect 一次调用
navigate('/other_page')
并解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.