Frequently Asked Questions About Codynn Next.js Full-Stack App Builder

22 answers covering everything from basics to advanced usage.

// Basics

What exactly is file-based routing in Next.js?

File-based routing is Next.js's model where the folder structure inside the app directory directly maps to URL paths — no router configuration file exists. A folder named 'about' with a page.tsx inside serves /about. Nested folders create nested routes: app/about/team/page.tsx serves /about/team. The folder hierarchy is the single source of truth for your URL structure.

Why must a page file be named exactly page.tsx?

Next.js identifies a route's page exclusively by the filename page.tsx (or page.js). This convention is non-negotiable — any other filename inside a route folder is treated as a co-located module, not a page. If you name it index.tsx or home.tsx, Next.js won't recognize it as a route and the URL will return a 404.

What is a slug in a Next.js dynamic route?

A slug is the conventional name for the dynamic segment parameter in a bracket folder like [slug]. It represents whatever value appears in that URL position — a blog post title, a product handle, or a user ID. The captured value is delivered to your page function via the params prop, which you await to extract it.

What does the children prop do in a layout.tsx file?

In a layout.tsx, the children prop is where the current page's content is slotted in. Anything passed between the layout's wrapper tags renders as children. This is how a layout wraps every page in its scope — you place shared UI like a navbar around {children}, and Next.js injects the active page in that position.

// How To

How do I read query string parameters like ?category=shoes in Next.js?

Create a client component with 'use client', import useSearchParams from 'next/navigation', call const searchParams = useSearchParams(), then searchParams.get('category') to read each value. Render this small client component inside your server page.tsx so the page stays a Server Component for data fetching while only the filter-reading logic runs on the client.

How do I redirect a user after a successful login in Next.js?

Make the login form a client component with 'use client', import useRouter from 'next/navigation', and on successful submission call router.push('/dashboard'). This is programmatic navigation in code — no Link component or button click required. useRouter also offers router.back() to mirror the browser back button. Note that useRouter requires 'use client'.

How do I organize routes without the folder name appearing in the URL?

Wrap the folder name in parentheses to create a Route Group, like app/(marketing)/pricing/page.tsx, which serves /pricing with no 'marketing' in the URL. Route Groups let you co-locate related routes, layouts, and components for code organization. You can also add a shared layout.tsx inside the group to wrap only those routes.

How do I self-host Google Fonts in Next.js?

Import fonts from 'next/font/google' instead of using a <link> tag to Google Fonts. Next.js downloads and self-hosts the font at build time, which is faster, eliminates layout shift, and removes the external request at runtime. Set this up in your root app/layout.tsx so the font applies across the entire app.

// Troubleshooting

Why is my dynamic route params returning a Promise instead of a value?

In modern Next.js, params in a dynamic route is a Promise and must be awaited. Accessing params.slug directly without await returns a pending Promise, not the value. Declare your page function as async, type params as Promise<{ slug: string }>, and write const { slug } = await params inside the function body.

Why does my component error when I use useState in Next.js?

Server Components can't use React hooks like useState, useEffect, useRouter, usePathname, or useSearchParams — attempting this causes a build or runtime error. Add 'use client' at the very top of the file to declare it a Client Component. Keep that client boundary small by extracting only the interactive part into its own component.

Why does clicking an internal link reload my whole page?

You're likely using a plain HTML <a> anchor tag, which reloads the entire page from scratch and discards all in-memory state. Replace every internal <a> with the Link component from 'next/link'. Link swaps only what changed and prefetches the destination in the background for instant navigation.

Why isn't my page showing up at the URL I expect?

Three common causes: the file isn't named exactly page.tsx, you forgot to export the component with 'export default', or you created an organizational folder inside app/ without wrapping it in parentheses — which turns it into an unintended URL segment. Check the filename, the default export, and whether stray folders are polluting your route tree.

Why is my list rendering incorrectly or re-rendering too much?

You've likely skipped the key prop or used the array index as the key. When rendering with map, every element needs a stable, unique key from the data, like item.id. The key lets React update, add, or remove only the exact element that changed. Index keys cause incorrect reconciliation when items reorder.

// Comparisons

How does Next.js App Router compare to using plain React with React Router?

Plain React with React Router ships all components to the browser and requires manual route config. The Next.js App Router uses folder-based routing, Server Components by default (less client JavaScript), built-in data fetching with await, server-rendered SEO metadata, API routes, and automatic loading/error/not-found states — all through filesystem conventions instead of code.

When should I use Link versus useRouter for navigation?

Use the Link component for navigation triggered by a user clicking an element — it's the default for menus, buttons, and anchors. Use the useRouter hook for programmatic navigation in code, such as redirecting after a successful login or form submission. Link works in Server Components; useRouter requires 'use client'.

When should I use a dynamic route versus a catch-all route?

Use a dynamic route [slug] when a single URL segment varies, like /blog/[slug] for one post title. Use a catch-all route [...slug] when the path can be arbitrarily deep, like /docs/getting-started/installation/windows. Dynamic routes deliver a string; catch-all routes deliver an array of strings, one per segment in order.

When should I use a Route Group versus a nested layout?

Use a Route Group (parentheses folder) when you want to organize files or apply a shared layout to multiple routes without adding a URL segment. Use a plain nested folder with its own layout.tsx when the URL segment is intentional. You can combine both — a Route Group with a layout.tsx scopes a shared header to those routes only.

// Advanced

When should I split data fetching between Server and Client Components?

Fetch data in Server Components by default — they can directly await data on the server, which keeps your API keys secure and reduces client JavaScript. Only move logic to a Client Component when it depends on browser state, such as reading query params with useSearchParams or responding to user interaction. Keep that client boundary as small as possible.

How do I keep a page server-rendered while still reading query parameters?

Extract the query-reading logic into a small Client Component that uses useSearchParams, then import and render that component inside your Server Component page.tsx. The page itself stays a Server Component — fetching data on the server — while only the tiny filter-reading component runs on the client. This pattern preserves server rendering and SEO.

How do I reconstruct a full path from a catch-all route's slug array?

In a [...slug]/page.tsx, type params as Promise<{ slug: string[] }> and await it. Each URL segment is one item in the array, in order. Use slug.join('/') to rebuild the full path for a data lookup, or slug.join('-') for a different format. One file handles all depth levels with no additional folders.

Why should I push 'use client' down the component tree?

Adding 'use client' to a high-level component forces everything it imports to also run on the client, inflating the JavaScript bundle. By pushing 'use client' down to small leaf-level components — only the interactive button or input — you keep the bulk of your UI as performant Server Components while isolating the minimal interactive logic.

Can I replace my backend entirely with Next.js API routes?

For most use cases, yes. Create route.ts files inside app directory folders and export GET, POST, and other HTTP-method functions returning NextResponse.json(). The same folder-based routing applies, so app/api/posts/route.ts answers /api/posts. This handles JSON APIs, form submissions, and database access without a separate server, though very large or specialized backends may still warrant a dedicated service.