Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what is the difference between [id].js and [...id].js and [[...id]].js ? #3

Open
utkarsh-1602 opened this issue Dec 17, 2023 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@utkarsh-1602
Copy link
Owner

  • [id].js: For example, a blog could include the following route pages/blog/[slug].js where [slug] is the Dynamic Segment for blog posts.
  • [...id].js: Dynamic Segments can be extended to catch-all subsequent segments by adding an ellipsis inside the brackets [...segmentName].
  • [[...id]].js: Catch-all Segments can be made optional by including the parameter in double square brackets: [[...segmentName]].
@utkarsh-1602 utkarsh-1602 added the question Further information is requested label Dec 17, 2023
@utkarsh-1602
Copy link
Owner Author

both [id].js and [...id].js are used for dynamic routing, but they serve slightly different purposes.

[id].js:

This is used for a single dynamic segment in the URL.
For example, if you have a route like /pages/blog/[id].js, it means that you can access the page with URLs like /blog/1, /blog/2, etc.The value captured by the dynamic segment (in this case, id) will be available as a parameter to the page component.

Example:


// pages/blog/[id].js
import { useRouter } from 'next/router';

const BlogPost = () => {
  const router = useRouter();
  const { id } = router.query;

  return <div>Blog Post {id}</div>;
};

export default BlogPost;

@utkarsh-1602
Copy link
Owner Author

[...id].js:

This is used for catching all subsequent segments in the URL.
For example, if you have a route like /pages/blog/[...id].js, it means that you can access the page with URLs like /blog/1, /blog/1/2, /blog/1/2/3, and so on.The values captured by the ellipsis notation will be available as an array in the id parameter.

Example:


// pages/blog/[...id].js
import { useRouter } from 'next/router';

const BlogPost = () => {
  const router = useRouter();
  const { id } = router.query;

  return <div>Blog Post {id.join('/')}</div>;
};

export default BlogPost;

@utkarsh-1602 utkarsh-1602 self-assigned this Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant