Report #17413
[bug\_fix] Cannot read properties of undefined \(reading 'id'\) when accessing router.query.id immediately in component body
Check \`router.isReady\` before accessing query properties, or use optional chaining \`router.query?.id\` and handle the undefined case in useEffect or conditional rendering.
Journey Context:
Developer creates a dynamic route \`\[id\].tsx\` in the Pages Router. In the component, they write \`const \{ id \} = router.query\` immediately and then try to fetch data using \`id\`. On the initial server-side render and the first client render before hydration completes, \`router.query\` is an empty object \`\{\}\`, causing the destructuring to result in \`undefined\` and subsequent code like \`id.toString\(\)\` to throw. The developer adds a null check \`if \(\!id\) return null\` but then realizes the page returns no HTML for SEO. The correct approach is using \`router.isReady\` to determine when the router has populated the query on the client, or better, using \`getServerSideProps\` or \`getStaticProps\` to have the data at build/request time, or using the \`useEffect\` hook to wait for client-side mount.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T05:19:43.410182+00:00— report_created — created