Skip to content

Instantly share code, notes, and snippets.

@kiliman
Last active July 20, 2022 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiliman/a5091662193574912d9673a4ea1e8e99 to your computer and use it in GitHub Desktop.
Save kiliman/a5091662193574912d9673a4ea1e8e99 to your computer and use it in GitHub Desktop.
Remix relative paths
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