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

Update readme #48

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
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
75 changes: 37 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

</div>

React Hook for syncing TanStack Table state with URL search params.
React Hook for syncing [TanStack Table](https://github.com/TanStack/table) state with URL search params.

https://github.com/user-attachments/assets/1f1b4a65-fdec-4a80-a5d5-783642befaa3

Expand Down Expand Up @@ -49,7 +49,6 @@ const table = useReactTable({
getCoreRowModel: getCoreRowModel(),
getFilteredRowModel: getFilteredRowModel(),
getSortedRowModel: getSortedRowModel(),
getPaginationRowModel: getPaginationRowModel(),
// ... other options
});
```
Expand Down Expand Up @@ -77,7 +76,6 @@ const table = useReactTable({
getCoreRowModel: getCoreRowModel(),
getFilteredRowModel: getFilteredRowModel(),
getSortedRowModel: getSortedRowModel(),
getPaginationRowModel: getPaginationRowModel(),
// ... other options
});
```
Expand Down Expand Up @@ -117,12 +115,11 @@ const table = useReactTable({
getCoreRowModel: getCoreRowModel(),
getFilteredRowModel: getFilteredRowModel(),
getSortedRowModel: getSortedRowModel(),
getPaginationRowModel: getPaginationRowModel(),
// ... other options
});
```

## Advanced
## Customization

- [Custom query param name](#custom-query-param-name)
- [Custom encoder/decoder](#custom-encoderdecoder)
Expand All @@ -148,6 +145,38 @@ const stateAndOnChanges = useTableSearchParams(router, {
});
```

### Custom default value

Default values can be customized.

"default value" means the value that is used as value of `state` when the query parameter is not present.

- [demo](https://tanstack-table-search-paramsexample-git-56132d-taro28s-projects.vercel.app/custom-default-value)
- [code](https://github.com/taro-28/tanstack-table-search-params/tree/main/examples/next-pages-router/src/pages/custom-default-value.tsx)

```tsx
const stateAndOnChanges = useTableSearchParams(router, {
sorting: {
// Sort by name in descending order when query parameter is not present
defaultValue: [{ id: "name", desc: true }],
},
});
```

If you want to set initial values for query parameters, either transition with the query parameter or add the query parameter after the transition, depending on the router you are using.

```tsx
// Transition with the query parameter
<Link href={{ pathname: "/users", query: { globalFilter: "foo" } }}>
Users
</Link>;

// Add the query parameter after the transition
useEffect(() => {
router.replace({ query: { globalFilter: "foo" } });
}, [router.replace]);
```

### Custom encoder/decoder

Encoder and decoder can be customized.
Expand Down Expand Up @@ -203,38 +232,6 @@ const stateAndOnChanges = useTableSearchParams(router, {
});
```

### Custom default value

Default values can be customized.

"default value" means the value that is used as value of `state` when the query parameter is not present.

- [demo](https://tanstack-table-search-paramsexample-git-56132d-taro28s-projects.vercel.app/custom-default-value)
- [code](https://github.com/taro-28/tanstack-table-search-params/tree/main/examples/next-pages-router/src/pages/custom-default-value.tsx)

```tsx
const stateAndOnChanges = useTableSearchParams(router, {
sorting: {
// Sort by name in descending order when query parameter is not present
defaultValue: [{ id: "name", desc: true }],
},
});
```

If you want to set initial values for query parameters, either transition with the query parameter or add the query parameter after the transition, depending on the router you are using.

```tsx
// Transition with the query parameter
<Link href={{ pathname: "/users", query: { globalFilter: "foo" } }}>
Users
</Link>;

// Add the query parameter after the transition
useEffect(() => {
router.replace({ query: { globalFilter: "foo" } });
}, [router.replace]);
```

## Supported

List of supported TanStack table states
Expand All @@ -253,9 +250,11 @@ List of supported TanStack table states
- [ ] rowPinning
- [ ] rowSelection

## TODO
## Roadmap

- [ ] support other table states
- [ ] disable specific state
- [ ] add examples for other routers

# License

Expand Down
75 changes: 37 additions & 38 deletions packages/tanstack-table-search-params/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

</div>

React Hook for syncing TanStack Table state with URL search params.
React Hook for syncing [TanStack Table](https://github.com/TanStack/table) state with URL search params.

https://github.com/user-attachments/assets/1f1b4a65-fdec-4a80-a5d5-783642befaa3

Expand Down Expand Up @@ -49,7 +49,6 @@ const table = useReactTable({
getCoreRowModel: getCoreRowModel(),
getFilteredRowModel: getFilteredRowModel(),
getSortedRowModel: getSortedRowModel(),
getPaginationRowModel: getPaginationRowModel(),
// ... other options
});
```
Expand Down Expand Up @@ -77,7 +76,6 @@ const table = useReactTable({
getCoreRowModel: getCoreRowModel(),
getFilteredRowModel: getFilteredRowModel(),
getSortedRowModel: getSortedRowModel(),
getPaginationRowModel: getPaginationRowModel(),
// ... other options
});
```
Expand Down Expand Up @@ -117,12 +115,11 @@ const table = useReactTable({
getCoreRowModel: getCoreRowModel(),
getFilteredRowModel: getFilteredRowModel(),
getSortedRowModel: getSortedRowModel(),
getPaginationRowModel: getPaginationRowModel(),
// ... other options
});
```

## Advanced
## Customization

- [Custom query param name](#custom-query-param-name)
- [Custom encoder/decoder](#custom-encoderdecoder)
Expand All @@ -148,6 +145,38 @@ const stateAndOnChanges = useTableSearchParams(router, {
});
```

### Custom default value

Default values can be customized.

"default value" means the value that is used as value of `state` when the query parameter is not present.

- [demo](https://tanstack-table-search-paramsexample-git-56132d-taro28s-projects.vercel.app/custom-default-value)
- [code](https://github.com/taro-28/tanstack-table-search-params/tree/main/examples/next-pages-router/src/pages/custom-default-value.tsx)

```tsx
const stateAndOnChanges = useTableSearchParams(router, {
sorting: {
// Sort by name in descending order when query parameter is not present
defaultValue: [{ id: "name", desc: true }],
},
});
```

If you want to set initial values for query parameters, either transition with the query parameter or add the query parameter after the transition, depending on the router you are using.

```tsx
// Transition with the query parameter
<Link href={{ pathname: "/users", query: { globalFilter: "foo" } }}>
Users
</Link>;

// Add the query parameter after the transition
useEffect(() => {
router.replace({ query: { globalFilter: "foo" } });
}, [router.replace]);
```

### Custom encoder/decoder

Encoder and decoder can be customized.
Expand Down Expand Up @@ -203,38 +232,6 @@ const stateAndOnChanges = useTableSearchParams(router, {
});
```

### Custom default value

Default values can be customized.

"default value" means the value that is used as value of `state` when the query parameter is not present.

- [demo](https://tanstack-table-search-paramsexample-git-56132d-taro28s-projects.vercel.app/custom-default-value)
- [code](https://github.com/taro-28/tanstack-table-search-params/tree/main/examples/next-pages-router/src/pages/custom-default-value.tsx)

```tsx
const stateAndOnChanges = useTableSearchParams(router, {
sorting: {
// Sort by name in descending order when query parameter is not present
defaultValue: [{ id: "name", desc: true }],
},
});
```

If you want to set initial values for query parameters, either transition with the query parameter or add the query parameter after the transition, depending on the router you are using.

```tsx
// Transition with the query parameter
<Link href={{ pathname: "/users", query: { globalFilter: "foo" } }}>
Users
</Link>;

// Add the query parameter after the transition
useEffect(() => {
router.replace({ query: { globalFilter: "foo" } });
}, [router.replace]);
```

## Supported

List of supported TanStack table states
Expand All @@ -253,9 +250,11 @@ List of supported TanStack table states
- [ ] rowPinning
- [ ] rowSelection

## TODO
## Roadmap

- [ ] support other table states
- [ ] disable specific state
- [ ] add examples for other routers

# License

Expand Down