Skip to content

Mismatch types between code and documentation for useSearchParams #518

Open
@Guillaume-Gaisne

Description

@Guillaume-Gaisne

I am using the recent useSearchParams hook to manage my query parameters in my application but it seems that there is a mismatch between what the documentation writes about how to use the hook and TypeScript that yells because of a void type when setting new params.

I am using this structure from the documentation:

// modify a specific search parameter
setSearchParams((prev) => {
  prev.set('tab', 'settings');
});

with something like this:

setSearchParams((prev) => {
  prev.set(name, val);
});

where name and val are the variables for the name of the query param to modify and val the value used for the modification.
In this context, TypeScript marks (prev) => as an error with this log:

Argument of type '(prev: URLSearchParams) => void' is not assignable to parameter of type 'string | URLSearchParams | string[][] | Record<string, string> | ((prev: URLSearchParams) => string | URLSearchParams | string[][] | Record<...> | undefined) | undefined'.
  Type '(prev: URLSearchParams) => void' is not assignable to type '(prev: URLSearchParams) => string | URLSearchParams | string[][] | Record<string, string> | undefined'.
    Type 'void' is not assignable to type 'string | URLSearchParams | string[][] | Record<string, string> | undefined'.

We can mitigate this error by adding a return prev after setting the new value but that might not be the expected behaviour.
Am I missing something here ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions