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

docs: useSearchParams #505

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ projects that use wouter: **[Ultra](https://ultrajs.dev/)**,
- [Customizing the location hook](#customizing-the-location-hook)
- [`useParams`: extracting matched parameters](#useparams-extracting-matched-parameters)
- [`useSearch`: query strings](#usesearch-query-strings)
- [`useSearchParams`: search parameters](#usesearchparams-search-parameters)
- [`useRouter`: accessing the router object](#userouter-accessing-the-router-object)
- [Component API](#component-api)

Expand Down Expand Up @@ -349,7 +350,6 @@ Use this hook to get the current search (query) string value. It will cause your
import { useSearch } from "wouter";

// returns "tab=settings&id=1"
// the hook for extracting search parameters is coming soon!
const searchString = useSearch();
```

Expand All @@ -361,6 +361,51 @@ For the SSR, use `ssrSearch` prop passed to the router.

Refer to [Server-Side Rendering](#server-side-rendering-support-ssr) for more info on rendering and hydration.

### `useSearchParams`: search parameters

Allow you to get and set any search parameters. The first returned value is a `URLSearchParams` object and the second returned value is a setter that accepts a `URLSearchParams` object with options.

```jsx
import { useSearchParams } from 'wouter-search';

const [searchParams, setSearchParams] = useSearchParams();

// extract a specific search parameter
const id = searchParams.get('id');

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

// override all search parameters
setSearchParams({
id: 1234,
tab: 'settings',
});

// by default, setSearchParams() will push a new history entry
// to avoid this, set `replace` option to `true`
setSearchParams(
(prev) => {
prev.set('order', 'desc');
},
{
replace: true,
},
);

// you can also pass a history state in options
setSearchParams(
(prev) => {
prev.set('foo', 'bar');
},
{
state: 'hello',
},
);
```

### `useRouter`: accessing the router object

If you're building advanced integration, for example custom location hook, you might want to get
Expand Down
Loading