-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useurlstate): useUrlState hook for react-router@6
- Loading branch information
1 parent
0ca0fbd
commit 1c7cd02
Showing
37 changed files
with
455 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,6 @@ rollup.config.js | |
*.test.tsx | ||
*.spec.ts | ||
*.spec.ts | ||
testUtils.ts | ||
dist | ||
exportsTest.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/example-react-router6/src/FewComponents/Component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use client'; | ||
import React from 'react'; | ||
import { useUrlState } from 'state-in-url/react-router'; | ||
|
||
import { stateShape } from './stateShape'; | ||
|
||
export const Component = () => { | ||
const { state, updateUrl } = useUrlState({ | ||
defaultState: stateShape, | ||
replace: false, | ||
}); | ||
|
||
return ( | ||
<div className="flex gap-2"> | ||
<h2>Per page select</h2> | ||
<select | ||
value={state.perPage} | ||
onChange={(ev) => | ||
updateUrl((curr) => ({ ...curr, perPage: +ev.target.value })) | ||
} | ||
className="text-black" | ||
data-testid="select" | ||
> | ||
<option>10</option> | ||
<option>20</option> | ||
<option>30</option> | ||
</select> | ||
</div> | ||
); | ||
}; |
13 changes: 13 additions & 0 deletions
13
packages/example-react-router6/src/FewComponents/Layout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react'; | ||
import { Outlet } from 'react-router-dom'; | ||
|
||
import { Component } from './Component'; | ||
|
||
export function Layout() { | ||
return ( | ||
<div> | ||
<Component /> | ||
<Outlet /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from 'react'; | ||
import { Link, useLocation, useParams } from 'react-router-dom'; | ||
|
||
import { Component } from './Component'; | ||
|
||
export function Page() { | ||
const { id } = useParams<'id'>(); | ||
const to = id === '1' ? '2' : '1'; | ||
const loc = useLocation(); | ||
const newUrl = `../${to}`; | ||
const newUrlFull = `${newUrl}${loc.search}`; | ||
|
||
return ( | ||
<div className="flex flex-col gap-4 h-[1250px]" data-testid="wrapper"> | ||
<h1>Page {id}</h1> | ||
|
||
<Component /> | ||
|
||
<Link to={newUrl} className="text-lg" data-testid="link"> | ||
To Page {to} | ||
</Link> | ||
|
||
<Link | ||
to={{ | ||
pathname: newUrl, | ||
search: loc.search, | ||
}} | ||
className="text-lg" | ||
data-testid="link-sp" | ||
> | ||
To Page {to} with QS | ||
</Link> | ||
<Link to={newUrlFull} data-testid="link-client"> | ||
To Page {to} with QS Client | ||
</Link> | ||
</div> | ||
); | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/example-react-router6/src/FewComponents/stateShape.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const stateShape = { | ||
perPage: 10, | ||
}; |
Oops, something went wrong.