Last active
July 20, 2022 17:49
-
-
Save kiliman/a5091662193574912d9673a4ea1e8e99 to your computer and use it in GitHub Desktop.
Remix relative paths
This file contains hidden or 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
function useRelativePath() { | |
const location = useLocation() | |
const base = new URL(`http://dummy${location.pathname}${location.search}`) | |
return (relativePath: string) => { | |
const relative = new URL(relativePath, base) | |
return `${relative.pathname}${relative.search}` | |
} | |
} | |
// path = /parent/child/?a=1 | |
const getRelative = useRelativePath() | |
console.log(getRelative('..?b=2')) // /parent/?b=2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment