简体   繁体   English

防止反应路由器在按下返回按钮时重新加载 API 调用。 Reactjs

[英]Prevent react router reloading API call when pressing back button. Reactjs

There are two pages - the main one with a list of elements and a page with a detailed description of the element.有两个页面 - 主页面包含元素列表,页面包含元素的详细描述。 Used react-router.使用 react-router。

<Switch>
    <Route exact path="/" component={PokemonCardContainer} />
    <Route path="/pokemon/:pokemonName" component={Pokemon} />
</Switch>

On the main page with the list, a request api is generated, which returns 20 elements.在带有列表的主页上,会生成一个请求 api,该请求返回 20 个元素。 Further, when we reach the end of the list, the api request is updated - another 20 items are loaded, etc.此外,当我们到达列表末尾时,更新了 api 请求 - 加载了另外 20 个项目,等等。 在此处输入图像描述

The detailed description page is implemented by the button "I Choose You!"详细描述页面由“我选择你”按钮实现。

<Link to={`pokemon/${pokemonName}`}>
   {pokemonName}, I Choose You!
</Link>

On the detailed description page there is a button "Return to Home"在详细描述页面上有一个按钮“返回首页”

const handleGoToHomePage = () => {
    props.history.push({
    pathname: "/",
   });
 };

Accordingly, when I press return to the main page, a new api request occurs and I get to the top of the page.因此,当我按返回到主页时,会出现一个新的 api 请求,然后我到达页面顶部。 but I need me to return to the element of the list I clicked on.但我需要返回到我单击的列表元素。 For example, if I clicked on the 60th element, I need to return to it.例如,如果我点击了第 60 个元素,我需要返回它。 I understand that I need to interrupt the api request when I return to the main?我了解返回主程序时需要中断 api 请求?

      /* States */
      const [pokemons, setPokemons] = useState([]); 
      const [loading, setLoading] = useState(true);
      const [error, setError] = useState(false);
      const [currentPage, setCurrentPage] = useState(
        "https://pokeapi.co/api/v2/pokemon"
      );
      const [nextPage, setNextPage] = useState("");
      const [hasMore, setHasMore] = useState(false);
      useEffect(() => {
        let cancel;
        const fetchData = () => {
          setLoading(true);
          setError(false);
          Axios({
            method: "GET",
            url: currentPage,
            cancelToken: new Axios.CancelToken((c) => (cancel = c)),
          })
          .then((res) => {
            setPokemons((prevPokemons) => {
            return [...prevPokemons, ...res.data.results];
          });
            setNextPage(res.data.next);
            setHasMore(res.data.results.length > 0);
            setLoading(false);
          })
          .catch((error) => {
            if (Axios.isCancel(error)) return;
            setError(true);
          });
        };
      fetchData();
      return () => cancel();
    }, [currentPage]);

I see two possibilities,我看到了两种可能,

You make the API call in a parent component that is not unmounted (eg a menu, or main component您在未卸载的父组件(例如菜单或主组件)中调用 API

Or you keep the data in a store using something like Redux .或者,您使用Redux 之类的东西将数据保存在存储中。 If your app is complex and needs to handle a complex state shared between components, I would go for this option.如果您的应用程序很复杂并且需要处理在组件之间共享的复杂 state,我会为这个选项使用 go。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM