Skip to content

Commit

Permalink
chore: add example of load JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
Daydreamer-riri committed May 22, 2024
1 parent d0b9b8b commit b245213
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/with-loader/src/docs/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"key": "THIS IS A TEST JSON"
}
4 changes: 4 additions & 0 deletions examples/with-loader/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const routes: RouteRecord[] = [
path: '/docs/:docs',
lazy: () => import('./pages/[docs]'),
},
{
path: '/json',
lazy: () => import('./pages/json'),
},
]

const routesWithLayout = [{
Expand Down
20 changes: 20 additions & 0 deletions examples/with-loader/src/pages/json.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useLoaderData } from 'react-router-dom'

export default function Docs() {
const json = useLoaderData() as typeof import('../docs/test.json')

return (
<div>{json.key}</div>
)
}

export const Component = Docs

export const entry = 'src/pages/json.tsx'

export async function loader() {
// If you use `import('../docs/test.json?raw')`, it will return a JSON string.
const json = (await import('../docs/test.json')).default

return json
}

0 comments on commit b245213

Please sign in to comment.