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

Internal state not reflecting actual state of query params after route change #862

Closed
keeganpotgieter opened this issue Jan 15, 2025 · 5 comments
Labels
adapters/react-router Uses the React Router adapter bug Something isn't working feature/useQueryStates
Milestone

Comments

@keeganpotgieter
Copy link

keeganpotgieter commented Jan 15, 2025

Context

What's your version of nuqs?

"nuqs": "^2.3.0"

What framework are you using?

  • ✅ React Router

Which version of your framework are you using?

"react-router-dom": "^6.28.1"

Description

The internal state of nuqs id not reflecting the actual state of query params after route change

Reproduction

export const useOrderFilters = ({ orderType}) => {
  const keyMap = React.useMemo(() => {
    const today = new Date();
    const DEFAULT_FROM_DATE = new Date(today.getFullYear(), today.getMonth() - 1, today.getDate()).getTime();
    const DEFAULT_TO_DATE = new Date().getTime();

    const isHistoric = orderType === 'historic';

    return {
      search: parseAsString.withDefault(''),
      ...(!isHistoric && { status: parseAsStringLiteral(CURRENT_ORDERS_FILTERS).withDefault('') }),
      ...(isHistoric && { fromDate: parseAsOptionalDate.withDefault(DEFAULT_FROM_DATE) }),
      ...(isHistoric && { toDate: parseAsOptionalDate.withDefault(DEFAULT_TO_DATE) }),
    };
  }, [orderType]);

  return useQueryStates(keyMap, { clearOnDefault: false });
};
<Route path="orders" element={<Layout />}>
        <Route index element={<OrdersPage tab="current" />} />
        <Route path="/history" element={<OrdersPage tab="historic" />} />
</Route>
export const OrdersPage = ({ tab }) => {
  const [orderType, setOrderType] = useQueryState(
    'type',
    parseAsStringEnum(['current', 'historic']).withDefault(tab ?? 'current'),
  );

  const [filters, setFilters] = useOrderFilters({ orderType });

  ....
}

When switching between /orders and /orders/history through react-router navigate, the filters will carry over.
E.g., Say we have search=bob when orderType is historical, and then we navigate, the url will not have any search params, but the search part of filters will remain the same.

@keeganpotgieter keeganpotgieter added the bug Something isn't working label Jan 15, 2025
@franky47
Copy link
Member

franky47 commented Jan 15, 2025

I see you're using a dynamic keyMap, could you please give this preview version a try and let me know if it works better? See issue #565 for reference (and PR #858).

pnpm add https://pkg.pr.new/nuqs@858

@franky47 franky47 added this to the 🪵 Backlog milestone Jan 15, 2025
@keeganpotgieter
Copy link
Author

The issue still occurs with the preview version.
I moved to a dynamic key map to see if that could help clear the caching—the issue is still present when not using a dynamic key map.

Thanks

@keeganpotgieter
Copy link
Author

Solved the issue by adding a key to the OrdersPage component.
Thanks

@franky47
Copy link
Member

Could this have been a caching thing from React Router, of it reusing the component rather than unmounting/remounting (which would explain your fix)?

Even so, it feels like there is a bug here, if the state is out of sync with the URL.

I'll see if I can reproduce it, could you please confirm what the issue was that the search params in the URL were cleared by a manual call to navigate but the internal state remained?

@franky47
Copy link
Member

franky47 commented Jan 16, 2025

I can't reproduce it in #863, could you please take a look at the test and see if it matches with the setup you had?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapters/react-router Uses the React Router adapter bug Something isn't working feature/useQueryStates
Projects
None yet
Development

No branches or pull requests

2 participants