diff --git a/examples/store/app/admin/pages/admin/products/[id].tsx b/examples/store/app/admin/pages/admin/products/[id].tsx index f12dc214ba..c59c3f8be5 100644 --- a/examples/store/app/admin/pages/admin/products/[id].tsx +++ b/examples/store/app/admin/pages/admin/products/[id].tsx @@ -1,12 +1,12 @@ import { Suspense } from "react" -import { Link, useRouter, useQuery, useParams } from "blitz" +import { Link, useRouter, useQuery, useParam } from "blitz" import getProduct from "app/products/queries/getProduct" import ProductForm from "app/products/components/ProductForm" function Product() { const router = useRouter() - const { id } = useParams() - const [product, { mutate }] = useQuery(getProduct, { where: { id: Number(id) } }) + const id = useParam("id", "number") + const [product, { mutate }] = useQuery(getProduct, { where: { id } }) return ( { const router = useRouter() - const __modelId__ = parseInt(router?.query.__modelId__ as string) + const __modelId__ = useParam('__modelId__', 'number') if (process.env.parentModel) { - const __parentModelId__ = parseInt(router?.query.__parentModelId__ as string) + const __parentModelId__ = useParam('__parentModelId__', 'number') } const [__modelName__] = useQuery(get__ModelName__, {where: {id: __modelId__}}) @@ -51,8 +51,7 @@ export const __ModelName__: React.FC = () => { const Show__ModelName__Page: React.FC = () => { if (process.env.parentModel) { - const router = useRouter() - const __parentModelId__ = parseInt(router?.query.__parentModelId__ as string) + const __parentModelId__ = useParam('__parentModelId__', 'number') } return ( diff --git a/packages/generator/templates/page/__modelIdParam__/edit.tsx b/packages/generator/templates/page/__modelIdParam__/edit.tsx index fd9695933a..ccfea03b33 100644 --- a/packages/generator/templates/page/__modelIdParam__/edit.tsx +++ b/packages/generator/templates/page/__modelIdParam__/edit.tsx @@ -6,9 +6,9 @@ import __ModelName__Form from 'app/__modelNames__/components/__ModelName__Form' export const Edit__ModelName__: React.FC = () => { const router = useRouter() - const __modelId__ = parseInt(router?.query.__modelId__ as string) + const __modelId__ = useParam('__modelId__', 'number') if (process.env.parentModel) { - const __parentModelId__ = parseInt(router?.query.__parentModelId__ as string) + const __parentModelId__ = useParam('__parentModelId__', 'number') } const [__modelName__, {mutate}] = useQuery(get__ModelName__, {where: {id: __modelId__}}) @@ -47,8 +47,7 @@ export const Edit__ModelName__: React.FC = () => { const Edit__ModelName__Page: React.FC = () => { if (process.env.parentModel) { - const router = useRouter() - const __parentModelId__ = parseInt(router?.query.__parentModelId__ as string) + const __parentModelId__ = useParam('__parentModelId__', 'number') } return ( diff --git a/packages/generator/templates/page/index.tsx b/packages/generator/templates/page/index.tsx index e9c0c9be3a..f9255ee6b7 100644 --- a/packages/generator/templates/page/index.tsx +++ b/packages/generator/templates/page/index.tsx @@ -1,6 +1,6 @@ import React, {Suspense} from 'react' if (process.env.parentModel) { - import {Head, Link, useRouter, useQuery} from 'blitz' + import {Head, Link, useQuery, useParam} from 'blitz' } else { import {Head, Link, useQuery} from 'blitz' } @@ -8,8 +8,7 @@ import get__ModelNames__ from 'app/__modelNames__/queries/get__ModelNames__' export const __ModelNames__List: React.FC = () => { if (process.env.parentModel) { - const router = useRouter() - const __parentModelId__ = parseInt(router?.query.__parentModelId__ as string) + const __parentModelId__ = useParam('__parentModelId__', 'number') const [__modelNames__] = useQuery(get__ModelNames__, { where: {__parentModel__: {id: __parentModelId__}}, orderBy: {id: 'desc'}, @@ -47,8 +46,7 @@ export const __ModelNames__List: React.FC = () => { const __ModelNames__Page: React.FC = () => { if (process.env.parentModel) { - const router = useRouter() - const __parentModelId__ = parseInt(router?.query.__parentModelId__ as string) + const __parentModelId__ = useParam('__parentModelId__', 'number') } return ( diff --git a/packages/generator/templates/page/new.tsx b/packages/generator/templates/page/new.tsx index 0a41f46851..65399952d8 100644 --- a/packages/generator/templates/page/new.tsx +++ b/packages/generator/templates/page/new.tsx @@ -1,12 +1,12 @@ import React from 'react' -import {Head, Link, useRouter} from 'blitz' +import {Head, Link, useRouter, useParam} from 'blitz' import create__ModelName__ from 'app/__modelNames__/mutations/create__ModelName__' import __ModelName__Form from 'app/__modelNames__/components/__ModelName__Form' const New__ModelName__Page: React.FC = () => { const router = useRouter() if (process.env.parentModel) { - const __parentModelId__ = parseInt(router?.query.__parentModelId__ as string) + const __parentModelId__ = useParam('__parentModelId__', 'number') } return (