Confused about NextJS, whether to use "app"-directory or "pages" and "index.tsx" or "page.tsx"

huangapple go评论61阅读模式
英文:

Confused about NextJS, whether to use "app"-directory or "pages" and "index.tsx" or "page.tsx"

问题

NextJS对我来说很令人困惑。所有官方示例和许多(大多数,全部?)我阅读过的教程,包括官方文档,都是使用一个pages目录来组织应用程序(如果需要,还有一个api目录用于后端服务)。它们声称pages目录下的每个文件都会自动反映出相应的路由名称。

然而,当使用npx create-next-app@latest创建一个新的NextJS应用程序时,我得到了一个完全不同的设置。它包含一个名为app的文件夹,而不是pages。此外,app文件夹包含一个名为page.tsx的文件,它在以前的pages文件夹中是index.tsx的同义词。

在这些教程和文档中,有一个概念是通过放置一个pages/_app.tsx文件来覆盖系统的App组件。我找不到如何在新的目录结构中执行此操作的任何信息。

教程和文档:

  • package.json
  • next.config.js
  • pages
    • _app.tsx
    • _document.tsx
    • about.tsx
    • index.tsx
  • public
    • favicon.ico

新的脚手架:

  • app
    • favicon.ico
    • globals.css
    • layout.tsx
    • page.tsx
  • next.config.hs
  • package.json

NextJS发生了什么?哪一个是正确的?是npx create-next-app@latest将创建的脚手架错误,还是有另一个版本的文档我找不到?

如果新的脚手架是正确的版本,我如何在app目录中创建新的路由和子页面?是否仍然有效使用api目录的概念?

希望有人能为我解释一下关于NextJS的最新发展。谢谢!

英文:

NextJS is confusing to me. All official examples and many (most, all?) tutorials that I've read including the official documentation are organizing the application with a pages-directory (and, if necessary, an api-directory for backend services). They claim that every file below the pages-directory will automatically reflect a corresponding route-name.

However, when creating a new NextJS app with npx create-next-app@latest, I get a total different setup. It contains an folder called app and not pages. Furthermore, the app-folder contain a file page.txs as a synonym what previously was index.tsx in the pages-folder.

In these tutorials and docs, there is the concept of overwriting the system-App-component with placing a file pages/_app.tsx.
I can't find anything how to do this with the new directory-scheme.

Tutorials and Docs:

- package.json
- next.config.js
+ pages
  - _app.tsx
  - _document.tsx
  - about.tsx
  - index.tsx
+ public
  - favicon.ico

New Scaffold:

+ app
  - favicon.ico
  - globals.css
  - layout.tsx
  - page.tsx
- next.config.hs
- package.json

What happened to NextJS? What is right? Is the scaffold wrong that npx create-next-app@latest will create or is there another version of documentation that I can't find?

In case of the new scaffold is the right version, how can I place new routes and sub-pages inside the app-directory? Is the concept of having an api-directory still valid?

I hope someone can shed some light here for me regarding latest evolvement of NextJS. Thank you!

答案1

得分: 4

在Next.js 13版本中,有许多变化。这是变化最多的版本,有一个详细的Vercel会议解释了每一个变化,在这里。首先,最重要的是组件现在默认是服务器端渲染的,因此前端和后端之间的分隔层现在更薄。但也有一些其他的beta版变化,比如使用turbo pack来编译您的应用程序。此外,您还可以将文件命名为layout或error,以便更轻松地处理设计、加载状态和错误。

关于应用程序目录,您可以在这里找到稳定版本的视频。只需将文件放在那里,它们就会变成路由。您还可以创建文件夹,它们将成为访问页面的路径的一部分。我相信您还可以使用[]来进行动态路由,如这里所解释的那样。以前的页面目录仍然有效,您可以与新的范例一起使用它。新的文档非常好,我建议您从这里开始https://nextjs.org/docs。然后,也许可以查看这个人的视频,他很好地解释了这个主题。

英文:

In Nextjs 13 version many things have changed. It is the version with more changes, there was a huge Vercel conf explaining each of those changes, here. The first of all is that components are server side by default, so the layer dividing front and back end is thinner now. But there are also other changes in beta, as turbo pack to compile your app. Also you can name your files with layout or error to handle design, loading state and errors easier.

About the app directory, you can find the stable release video here. just put files there and they will become routes. You can also create folders and they will become part of the path to access a page. I believe you can also have dynamic routing using [], as explained here. The previous page directory still works, and you can use it along side with the new paradigm. The new documentation is very good and I recommend you to start with that https://nextjs.org/docs. Then, maybe check out this guy's videos that explains very well this topic.

huangapple
  • 本文由 发表于 2023年6月2日 04:56:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定