简体   繁体   English

将 React Typescript 应用程序转换为 Nextjs

[英]Converting React Typescript application to Nextjs

I am new to Nextjs and I have built a web application using Reactjs + Typescript and I want to convert this to Nextjs.我是 Nextjs 的新手,我已经使用 Reactjs + Typescript 构建了一个 Web 应用程序,我想将其转换为 Nextjs。 I am confused about the file structure of what goes where and the installation part.我对去哪里和安装部分的文件结构感到困惑。 The current web application (Reactjs with Typescript) has a database(Postdb) and functionalities like:当前的 Web 应用程序(带有 Typescript 的 Reactjs)有一个数据库(Postdb)和如下功能:

  1. create a new note创建新笔记
  2. comment on the note注释注释
  3. like and dislike the note.喜欢和不喜欢笔记。

Is there a way that I can convert this existing project smoothly to nextjs?有没有办法可以将这个现有项目顺利转换为nextjs?

Also this is my file structure这也是我的文件结构


(New-App)
 * node_modules
 * public
   * index.html
   * manifest.json
   * robots.txt
 * src
   * App.tsx
   * db.tsx
   * serviceWorker.ts
   * index.tsx
   * components
       *Notes.tsx
       *Header.tsx
       *List.tsx

   *pages
       *index.tsx
       *Quest.tsx
       *Page.tsx

    * test (has all the test files)
    * images

 *build

Nextjs's pages directory is associated with route (see this link ), and it'd be better spliting SSR logic from each react component. Nextjs 的 pages 目录与 route 相关联(见此链接),最好将 SSR 逻辑从每个 react 组件中分离出来。
So I prefer pages file have route and SSR logic and components file have templating and client-side logic.所以我更喜欢页面文件有路由和 SSR 逻辑,组件文件有模板和客户端逻辑。

For example.例如。

pages/index.tsx页/index.tsx

import Notes from '../components/notes'
function HomePage({ notes }) {
  return (
    <div>
      <Notes notes={notes}></Notes>
    </div>
  )
}

HomePage.getInitialProps = async ({ req }) => {
  // DO SSR
  const res = await fetch('https://some/api/endpoint')
  const json = await res.json()
  return { notes: json.notes }
}

export default HomePage

components/notes/index.tsx组件/注释/index.tsx

import Note from './Note';
export default (props) => {
  return (
    <div>
    {
      props.notes && props.notes.map((note) => <Note note={note}></Note>)
    }
    </div>
  )
}

components/notes/Note.tsx组件/注释/Note.tsx

export default (props) => {
  return (
    <div>
      <p>comment {props.note.comment}</p>
      <p>like {props.note.like}</p>
      <p>dislike {props.note.dislike}</p>
    </div>
  )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM