-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- react-router examples - Removed Router.svelte (in favor of using react:RouterProvider directly) - Cleaner & optional RouterContext
- Loading branch information
Showing
29 changed files
with
933 additions
and
724 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
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,10 +1,26 @@ | ||
import * as React from "react"; | ||
import RouterContext from "./RouterContext.js"; | ||
import RouterContext, { type RouterContextType } from "./RouterContext.js"; | ||
|
||
export default function useRouterContext() { | ||
export default function useRouterContext(): RouterContextType { | ||
const context = React.useContext(RouterContext); | ||
if (!context) { | ||
throw new Error("Component was not wrapped inside a <Router>"); | ||
console.warn("Component was not wrapped inside a <react:RouterProvider>"); | ||
return { | ||
url: new URL( | ||
typeof window !== "undefined" | ||
? window.location.href | ||
: "http://localhost/" | ||
), | ||
params: {}, | ||
goto, | ||
}; | ||
} | ||
return context; | ||
} | ||
|
||
function goto(url: string) { | ||
console.warn( | ||
"No access to <react:RouterProvider>, falling back to using browser navigation" | ||
); | ||
window.location.href = url; | ||
} |
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 |
---|---|---|
@@ -1,6 +1,17 @@ | ||
import * as React from "react"; | ||
import useRouterContext from "./internal/useRouterContext.js"; | ||
|
||
export default function useHistory() { | ||
const { history } = useRouterContext(); | ||
return history; | ||
const { goto } = useRouterContext(); | ||
return React.useMemo( | ||
() => ({ | ||
push(url: string) { | ||
goto(url); | ||
}, | ||
replace(url: string) { | ||
goto(url, { replaceState: true }); | ||
}, | ||
}), | ||
[goto] | ||
); | ||
} |
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 |
---|---|---|
@@ -1,15 +1,14 @@ | ||
import * as React from "react"; | ||
import useRouterContext from "./internal/useRouterContext.js"; | ||
import locationToUrl from "./internal/locationToUrl.js"; | ||
import type { Location } from "./types"; | ||
|
||
export default function useLocation(): Location { | ||
const { | ||
base, | ||
location: { pathname, search, hash }, | ||
url: { pathname, search, hash }, | ||
} = useRouterContext(); | ||
|
||
return React.useMemo(() => { | ||
return locationToUrl({ pathname, search, hash }, base); | ||
}, [hash, pathname, search, base]); | ||
return React.useMemo( | ||
() => ({ pathname, search, hash }), | ||
[hash, pathname, search] | ||
); | ||
} |
Oops, something went wrong.