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

Open
keeganpotgieter opened this issue Jan 15, 2025 · 1 comment
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
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