Search code examples
reactjsreact-hooksfetchreact-query

How to stop fetching start api url after going back from route? React/React-query


Problem: When I click pokemon details on next pages, after going back, app is fetching everytime first 12 pokemons.

I want after going back to stay on current page.

Link to code sanbox https://codesandbox.io/s/bitter-pine-57iwo?file=/src/components/Pokemon/PokemonList.js


Solution

  • If you want to keep your pagination state, i'd recommend use use a state management tool, such as Redux, Even React context can handle this one.

    As you are using the prev/next url approach, i don't think it would be good to store this values on the queryParams from the list route.

    So, yeah, go for state management.

    https://github.com/reduxjs/redux-thunk https://reactjs.org/docs/context.html