UNPKG

@tanstack/react-router

Version:

Modern and scalable routing for React applications

1 lines 3.38 kB
{"version":3,"file":"not-found.cjs","names":[],"sources":["../../src/not-found.tsx"],"sourcesContent":["import * as React from 'react'\nimport { isNotFound } from '@tanstack/router-core'\nimport { isServer } from '@tanstack/router-core/isServer'\nimport { useStore } from '@tanstack/react-store'\nimport { CatchBoundary } from './CatchBoundary'\nimport { useRouter } from './useRouter'\nimport type { ErrorInfo } from 'react'\nimport type { NotFoundError } from '@tanstack/router-core'\n\nexport function CatchNotFound(props: {\n fallback?: (error: NotFoundError) => React.ReactElement\n onCatch?: (error: Error, errorInfo: ErrorInfo) => void\n children: React.ReactNode\n}) {\n const router = useRouter()\n\n if (isServer ?? router.isServer) {\n const pathname = router.stores.location.state.pathname\n const status = router.stores.status.state\n const resetKey = `not-found-${pathname}-${status}`\n\n return (\n <CatchBoundary\n getResetKey={() => resetKey}\n onCatch={(error, errorInfo) => {\n if (isNotFound(error)) {\n props.onCatch?.(error, errorInfo)\n } else {\n throw error\n }\n }}\n errorComponent={({ error }) => {\n if (isNotFound(error)) {\n return props.fallback?.(error)\n } else {\n throw error\n }\n }}\n >\n {props.children}\n </CatchBoundary>\n )\n }\n\n // TODO: Some way for the user to programmatically reset the not-found boundary?\n // eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static\n const pathname = useStore(\n router.stores.location,\n (location) => location.pathname,\n )\n // eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static\n const status = useStore(router.stores.status, (status) => status)\n const resetKey = `not-found-${pathname}-${status}`\n\n return (\n <CatchBoundary\n getResetKey={() => resetKey}\n onCatch={(error, errorInfo) => {\n if (isNotFound(error)) {\n props.onCatch?.(error, errorInfo)\n } else {\n throw error\n }\n }}\n errorComponent={({ error }) => {\n if (isNotFound(error)) {\n return props.fallback?.(error)\n } else {\n throw error\n }\n }}\n >\n {props.children}\n </CatchBoundary>\n )\n}\n\nexport function DefaultGlobalNotFound() {\n return <p>Not Found</p>\n}\n"],"mappings":";;;;;;;;;;AASA,SAAgB,cAAc,OAI3B;CACD,MAAM,SAAS,kBAAA,WAAW;AAE1B,KAAI,+BAAA,YAAY,OAAO,UAAU;EAG/B,MAAM,WAAW,aAFA,OAAO,OAAO,SAAS,MAAM,SAEP,GADxB,OAAO,OAAO,OAAO;AAGpC,SACE,iBAAA,GAAA,kBAAA,KAAC,sBAAA,eAAD;GACE,mBAAmB;GACnB,UAAU,OAAO,cAAc;AAC7B,SAAA,GAAA,sBAAA,YAAe,MAAM,CACnB,OAAM,UAAU,OAAO,UAAU;QAEjC,OAAM;;GAGV,iBAAiB,EAAE,YAAY;AAC7B,SAAA,GAAA,sBAAA,YAAe,MAAM,CACnB,QAAO,MAAM,WAAW,MAAM;QAE9B,OAAM;;aAIT,MAAM;GACO,CAAA;;CAYpB,MAAM,WAAW,cAAA,GAAA,sBAAA,UALf,OAAO,OAAO,WACb,aAAa,SAAS,SACxB,CAGsC,IAAA,GAAA,sBAAA,UADf,OAAO,OAAO,SAAS,WAAW,OAAO;AAGjE,QACE,iBAAA,GAAA,kBAAA,KAAC,sBAAA,eAAD;EACE,mBAAmB;EACnB,UAAU,OAAO,cAAc;AAC7B,QAAA,GAAA,sBAAA,YAAe,MAAM,CACnB,OAAM,UAAU,OAAO,UAAU;OAEjC,OAAM;;EAGV,iBAAiB,EAAE,YAAY;AAC7B,QAAA,GAAA,sBAAA,YAAe,MAAM,CACnB,QAAO,MAAM,WAAW,MAAM;OAE9B,OAAM;;YAIT,MAAM;EACO,CAAA;;AAIpB,SAAgB,wBAAwB;AACtC,QAAO,iBAAA,GAAA,kBAAA,KAAC,KAAD,EAAA,UAAG,aAAa,CAAA"}