-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new 'useCreateHref' and 'useResolvePath' hooks
- Loading branch information
Showing
11 changed files
with
197 additions
and
15 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"react-router": minor | ||
"react-router-dom": minor | ||
"react-router-dom-v5-compat": minor | ||
"react-router-native": minor | ||
--- | ||
|
||
Add new `useCreateHref` and `useResolvePath` hooks for deferred creation of hrefs and paths. |
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,24 @@ | ||
--- | ||
title: useCreateHref | ||
--- | ||
|
||
# `useCreateHref` | ||
|
||
<details> | ||
<summary>Type declaration</summary> | ||
|
||
```tsx | ||
declare function useCreateHref(): ( | ||
to: To, | ||
options?: { relative?: RelativeRoutingType } | ||
): string; | ||
``` | ||
|
||
</details> | ||
|
||
The `useCreateHref` hook returns function which will return a URL when called that may be used to link to the given `to` location, even outside of React Router. | ||
|
||
> **Tip:** | ||
> | ||
> You may be interested in taking a look at the source for the `useHref` | ||
> component in `react-router` to see how it uses `useCreateHref` internally. |
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,24 @@ | ||
--- | ||
title: useResolvePath | ||
--- | ||
|
||
# `useResolvePath` | ||
|
||
<details> | ||
<summary>Type declaration</summary> | ||
|
||
```tsx | ||
declare function useResolvePath(): ( | ||
to: To, | ||
options?: { relative?: RelativeRoutingType } | ||
): Path; | ||
``` | ||
|
||
</details> | ||
|
||
This hook returns a function that resolves the `pathname` of the location in the given `to` value against the pathname of the current location. | ||
|
||
> **Tip:** | ||
> | ||
> You may be interested in taking a look at the source for the `useResolvedPath` | ||
> component in `react-router` to see how it uses `useResolvePath` internally. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as React from "react"; | ||
import * as TestRenderer from "react-test-renderer"; | ||
import { MemoryRouter, Routes, Route, useCreateHref } from "react-router"; | ||
|
||
function ShowHref({ to }: { to: string }) { | ||
const createHref = useCreateHref(); | ||
return <pre>{createHref(to)}</pre>; | ||
} | ||
|
||
describe("useCreateHref", () => { | ||
describe("to a child route", () => { | ||
it("returns the correct href", () => { | ||
let renderer: TestRenderer.ReactTestRenderer; | ||
TestRenderer.act(() => { | ||
renderer = TestRenderer.create( | ||
<MemoryRouter initialEntries={["/courses"]}> | ||
<Routes> | ||
<Route | ||
path="courses" | ||
element={<ShowHref to="advanced-react" />} | ||
/> | ||
</Routes> | ||
</MemoryRouter> | ||
); | ||
}); | ||
|
||
expect(renderer.toJSON()).toMatchInlineSnapshot(` | ||
<pre> | ||
/courses/advanced-react | ||
</pre> | ||
`); | ||
}); | ||
}); | ||
}); |
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,33 @@ | ||
import * as React from "react"; | ||
import * as TestRenderer from "react-test-renderer"; | ||
import type { Path } from "react-router"; | ||
import { MemoryRouter, Routes, Route, useResolvePath } from "react-router"; | ||
|
||
function ShowResolvedPath({ path }: { path: string | Path }) { | ||
const resolvePath = useResolvePath(); | ||
return <pre>{JSON.stringify(resolvePath(path))}</pre>; | ||
} | ||
|
||
describe("useResolvePath", () => { | ||
it("path string resolves correctly", () => { | ||
let renderer: TestRenderer.ReactTestRenderer; | ||
TestRenderer.act(() => { | ||
renderer = TestRenderer.create( | ||
<MemoryRouter initialEntries={["/"]}> | ||
<Routes> | ||
<Route | ||
path="/" | ||
element={<ShowResolvedPath path="/home?user=mj#welcome" />} | ||
/> | ||
</Routes> | ||
</MemoryRouter> | ||
); | ||
}); | ||
|
||
expect(renderer.toJSON()).toMatchInlineSnapshot(` | ||
<pre> | ||
{"pathname":"/home","search":"?user=mj","hash":"#welcome"} | ||
</pre> | ||
`); | ||
}); | ||
}); |
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