Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to respond to query parameter changes with a resource update #167

Open
anacierdem opened this issue Oct 27, 2022 · 2 comments
Open

Comments

@anacierdem
Copy link

As it is also implied by the documentation;

... Note: resources are only refreshed on route change. The router does not poll or update resources in the background. Navigation within the same route, e.g. query param change, will not trigger a refresh of resources.
The resource is not refreshed when the query is changed by;

  const [page = '1', setPage] = useQueryParam('page');
  // call setPage() somewhere

Only way to force a refetch seems to be via an additional;

const resource = useResource(routeResource);
// resource.refetch() forces a proper update

Then, in resource's getData function the query is not properly updated and only way to practically access it is via window.location, which is an implementation detail and should not be ideally used:

getData: (routerContext) => {
  const params = new URLSearchParams(window.location.search);
  const page = params.get('page') || '1';
  // want to use page here, routerContext.query is a stale value from the previous route change for some reason
}

Ideally I would expect it to get triggered for query param changes as well. As it has probably now became a public behaviour maybe we can do it behind a boolean option?

Even this is not implemented, routerContext should get properly updated before getData is called so that we have a workaround.

@anacierdem
Copy link
Author

There is a potential workaround: using the query parameter in the route key. This is something we will experiment with.

@anacierdem
Copy link
Author

Using a cache key that gets invalidated based on query parameter solves the issue. Still, updating documentation would be great. I will try to have a look if I can find the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant